Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何隐藏QComboBox项而不是清除它们_Python_Pyqt - Fatal编程技术网

Python 如何隐藏QComboBox项而不是清除它们

Python 如何隐藏QComboBox项而不是清除它们,python,pyqt,Python,Pyqt,我找不到隐藏QComboBox项的方法。到目前为止,筛选其项的唯一方法是删除现有项(使用.clear()方法)。然后使用其.addItem()方法再次重建整个QComboBox 我宁愿暂时隐藏这些物品。当需要把它们放回去的时候。 是否可以在QCombobox上隐藏/取消隐藏项目?使用setVisible()更改对象的可见性: .setVisible(False) # Not Visible .setVisible(True) # Visible 您可以使用removietem()方法从QCom

我找不到隐藏
QComboBox
项的方法。到目前为止,筛选其项的唯一方法是删除现有项(使用
.clear()
方法)。然后使用其
.addItem()
方法再次重建整个
QComboBox

我宁愿暂时隐藏这些物品。当需要把它们放回去的时候。 是否可以在QCombobox上隐藏/取消隐藏项目?

使用
setVisible()
更改对象的可见性:

.setVisible(False) # Not Visible
.setVisible(True) # Visible

您可以使用
removietem()
方法从
QComboBox
中删除项目

void qcombox::removietem(int索引)

从组合框中删除给定索引处的项。如果删除索引,则此操作将更新当前索引

如果索引超出范围,则此函数不执行任何操作

如果不知道索引,请使用
findText()
方法


QComboBox
项没有隐藏/取消隐藏方法。

虽然没有直接的方法隐藏
QComboBox
项,但您可以使用
QComboBox::setItemData
并将大小设置为(0,0)来隐藏
OMQComboBox
项:

comboBox->setItemData(row, QSize(0,0), Qt::SizeHintRole);

要再次显示项目,请执行以下操作:

comboBox->setItemData(row, QVariant(), Qt::SizeHintRole);

注意:更改大小简介在OS X上不起作用。

如果有人仍在寻找答案:

默认情况下,
QComboBox
使用
QListView
显示弹出列表,并且
QListView
具有
setRowHidden()
方法:

qobject_cast<QListView *>(comboBox->view())->setRowHidden(0, true);

以@kef回答的内容为基础:

(借口Python问题的C++)< /P> 默认情况下,QComboBox将使用QListView作为视图,因此您可以执行以下操作:

QListView* view = qobject_cast<QListView *>(combo->view());
Q_ASSERT(view != nullptr);
view->setRowHidden(row, true);

我遇到这个问题是因为我对隐藏功能的缺乏感到沮丧,因为隐藏功能会使项目索引等保持不变。 我根据@Kef和@CJCombrink的答案让它工作。这基本上只是一个python翻译

我对qobject_的演员阵容有问题。 通过将.setView(QListView())设置为QComboBox解决了这个问题

combo=QComboBox()
combo.setView(QListView())
隐藏:

取消隐藏:

combo.view().setRowHidden(rowindex,False)
tmp_item=combo.model().item(rowindex)
tmp_item.setFlags(tmp_item.flags() | Qt.ItemIsEnabled)
我决定对QComboBox进行子类化,并添加隐藏功能。Bellow是测试的一个使用示例。 欢迎您使用它。我不能保证

import sys
from PySide2.QtWidgets import *
from PySide2.QtCore import *
from PySide2.QtGui import *

#subclassed QComboBox with added hide row functionality
class ComboBox_whide(QComboBox):
    def __init__(self):
        super().__init__()
        self.setView(QListView())#default self.view() is a QAbstractItemView object which is missing setRowHidden, therefore a QListView needs to be set 
    def hide_row_set(self,row,value=True):
        """sets the row accesibility
        value=True hides the row"""
        self.view().setRowHidden(row,value)#hides the item from dropdown, however the item is stil accesible by moving down with arrow keys or mouse wheel. The following disables solves that
        tmp_item=self.model().item(row)
        if value:#hide -> disable
            tmp_item.setFlags(tmp_item.flags() & ~Qt.ItemIsEnabled)
        else:#enable
            tmp_item.setFlags(tmp_item.flags() | Qt.ItemIsEnabled)
    def hide_row_toggle(self,row):
        """toggles the row accesibility"""
        if self.view().isRowHidden(row):#is hidden, therefore make available
            self.hide_row_set(row,False)
        else:#is not hidden, therefore hide
            self.hide_row_set(row,True)

class Main(QMainWindow):
    def __init__(self):
        super().__init__()
        cwidg=QWidget()
        clayer=QVBoxLayout()
        cwidg.setLayout(clayer)
        self.setCentralWidget(cwidg)
        #button for testing
        self.btn=QPushButton('Button')
        self.btn.setCheckable(True)
        clayer.addWidget(self.btn)
        
        #subclassed QComboBox
        self.combo=ComboBox_whide()
        for n in range(3):#add 3 items with tooltips
            self.combo.addItem('item%i'%n)
            self.combo.setItemData(n,'tip%i'%n,Qt.ToolTipRole)
        clayer.addWidget(self.combo)
        
        #button test function - choose either or for testing
        self.btn.clicked.connect(self.btn_clicked)
        #uncomment for add/remove example self.btn.clicked.connect(self.remove_add_item)
        
    def btn_clicked(self):
        self.combo.hide_row_toggle(1)
    def remove_add_item(self):# here for naive comparison and to show why removing and adding is not ok
        if self.combo.count()==3:
            self.combo.removeItem(1)
        else:
            self.combo.addItem('new')#new "item1" withouth the ToolTip
        
if __name__ == '__main__':
    app = QApplication.instance()
    if app is None:#Pyside2 ipython notebook check
        app = QApplication(sys.argv)
    main = Main()
    main.show()
    app.exec_()
    

如何访问组合框项目,以便将其设置为可见(真)或(假)…我以为您指的是框中的整个项目集。我不确定是否可以隐藏单个项目。删除后再添加有什么问题?您在说“.addItem()”?这在“QListWidget”中?如果他想“隐藏”组合框中的单个项目,最好将其删除,然后在必要时再添加。这是关于隐藏条目,而不是删除它…请参阅下面关于如何操作的答案(qobject_cast(组合框->视图())->setRowHidden(0,true);)如何使其再次可见?对我来说很有用,尽管修正很小,但重新启用标志应该是二进制的“或”:item->setFlags(item->flags()| Qt::ItemIsEnabled);当您看到滚轮出现问题时,是否使用了可编辑的组合框?在不可编辑的组合上,只需使用view->setRowHidden(..)即可。Qt。5.9.3@Alchete是的,组合框是可编辑的,Qt5.9.2可能是Qt5.9.2中的一个bug,在Qt5.9.3中修复了(目前无法测试)我必须以以下方式强制转换视图:qobject_cast(组合框->视图())->setRowHidden(0,true);事实上,这应该是公认的答案--此方法还有另一个警告:^虽然项目将不可见且不可用于鼠标单击,但仍可通过滚轮和键盘访问。--应该在注释之前再次阅读整个线程=)你是对的,感谢您指出,该项目也必须被禁用,以防止车轮和箭头键的选择只需要C++来将由<代码> -VIEW()/代码>返回的通用代码<代码> QAcExcTimeVIEW/COD>类型具体化(<代码> qListVIEW/COD>),在Python上不需要,只需在COMBOX实例上调用<代码>视图/代码>,即可得到所需的<代码> QListVIEW/COD>对象实例。
combo.view().setRowHidden(rowindex,True)
tmp_item=combo.model().item(rowindex)
tmp_item.setFlags(tmp_item.flags() & ~Qt.ItemIsEnabled)
combo.view().setRowHidden(rowindex,False)
tmp_item=combo.model().item(rowindex)
tmp_item.setFlags(tmp_item.flags() | Qt.ItemIsEnabled)
import sys
from PySide2.QtWidgets import *
from PySide2.QtCore import *
from PySide2.QtGui import *

#subclassed QComboBox with added hide row functionality
class ComboBox_whide(QComboBox):
    def __init__(self):
        super().__init__()
        self.setView(QListView())#default self.view() is a QAbstractItemView object which is missing setRowHidden, therefore a QListView needs to be set 
    def hide_row_set(self,row,value=True):
        """sets the row accesibility
        value=True hides the row"""
        self.view().setRowHidden(row,value)#hides the item from dropdown, however the item is stil accesible by moving down with arrow keys or mouse wheel. The following disables solves that
        tmp_item=self.model().item(row)
        if value:#hide -> disable
            tmp_item.setFlags(tmp_item.flags() & ~Qt.ItemIsEnabled)
        else:#enable
            tmp_item.setFlags(tmp_item.flags() | Qt.ItemIsEnabled)
    def hide_row_toggle(self,row):
        """toggles the row accesibility"""
        if self.view().isRowHidden(row):#is hidden, therefore make available
            self.hide_row_set(row,False)
        else:#is not hidden, therefore hide
            self.hide_row_set(row,True)

class Main(QMainWindow):
    def __init__(self):
        super().__init__()
        cwidg=QWidget()
        clayer=QVBoxLayout()
        cwidg.setLayout(clayer)
        self.setCentralWidget(cwidg)
        #button for testing
        self.btn=QPushButton('Button')
        self.btn.setCheckable(True)
        clayer.addWidget(self.btn)
        
        #subclassed QComboBox
        self.combo=ComboBox_whide()
        for n in range(3):#add 3 items with tooltips
            self.combo.addItem('item%i'%n)
            self.combo.setItemData(n,'tip%i'%n,Qt.ToolTipRole)
        clayer.addWidget(self.combo)
        
        #button test function - choose either or for testing
        self.btn.clicked.connect(self.btn_clicked)
        #uncomment for add/remove example self.btn.clicked.connect(self.remove_add_item)
        
    def btn_clicked(self):
        self.combo.hide_row_toggle(1)
    def remove_add_item(self):# here for naive comparison and to show why removing and adding is not ok
        if self.combo.count()==3:
            self.combo.removeItem(1)
        else:
            self.combo.addItem('new')#new "item1" withouth the ToolTip
        
if __name__ == '__main__':
    app = QApplication.instance()
    if app is None:#Pyside2 ipython notebook check
        app = QApplication(sys.argv)
    main = Main()
    main.show()
    app.exec_()