我希望能够从它的下拉菜单中选择多个QComboBox的项目(使用Shift或Alt+click从当前选择中添加/提取)。
ComboBox不会像默认情况那样回退(或自崩溃)。相反,它将保持打开状态,以便用户能够Shift+click更多的组合框项来添加或从选择中提取它们。最后,当用户对所选内容满意时,将按下Enter键来“确认”所选内容。
这里是一张Photoshoped图像,展示了我想要实现的“多选择”QComboBox的概念:

下面的代码创建了几乎每个事件都公开的QComboBox。但我唯一看到的“表演”是onMousePressEvent()。当用户从下拉菜单中选择项目时,我找不到追踪的方法.
编辑:
一小时后,我了解到可以通过QComboBox的.view().setSelectionMode(3)将其设置为多个选择。
然后,可以使用相同的.view()来查询组合框选定的项:
selectedIndexes=self.view().selectionModel().selectedIndexes() (其中self本身就是一个组合体)
有一种使用selectionModel.select(idx, QtGui.QItemSelectionModel.Select)选择组合框项的方法
但到目前为止我没能做到……
from PyQt4 import QtCore, QtGui
app = QtGui.QApplication([])
class Combo(QtGui.QComboBox):
def __init__(self, *args, **kwargs):
super(Combo, self).__init__()
self.addItems(['Item_1','Item_2','Item_3','Item_4','Item_5'])
self.view=self.view()
self.view.setSelectionMode(3)
self.activated.connect(self.clicked)
self.show()
def clicked(self, arg=None):
selectionModel=self.view.selectionModel()
selectedIndexes=selectionModel.selectedIndexes()
for idx in selectedIndexes:
selectionModel.select(idx, QtGui.QItemSelectionModel.Select)
print 'selecting idx: %s'%idx
self.showPopup()
tree=Combo()
sys.exit(app.exec_())发布于 2014-09-09 02:47:36
另外,您可以使用简单的图标来检查状态。比QtGui.QStandardItem更容易。方法QIcon QComboBox.itemIcon (self, int index)和方法QComboBox.setItemIcon (self, int index, QIcon icon)可在QtGui.QComboBox类中使用;
import sys
from PyQt4 import QtGui
class QCustomComboBox (QtGui.QComboBox):
CHECK_QICON = QtGui.QIcon.fromTheme('call-start')
UNCHECK_QICON = QtGui.QIcon.fromTheme('call-stop')
def __init__(self, *args, **kwargs):
super(QCustomComboBox, self).__init__(*args, **kwargs)
listsItem = ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5']
for index in range(0, len(listsItem)):
self.addItem(listsItem[index])
self.setItemIcon(index, self.UNCHECK_QICON)
self.activated.connect(self.customActivated)
def hidePopup (self):
pass # Force only ESC button to exit.
def customActivated (self, index):
# self.blockSignals(True) # Force first index only
# self.setCurrentIndex(0)
# self.blockSignals(False)
stateQIcon = self.itemIcon(index)
newQIcon = {
self.CHECK_QICON.name() : self.UNCHECK_QICON,
self.UNCHECK_QICON.name() : self.CHECK_QICON,
} [stateQIcon.name()]
self.setItemIcon(index, newQIcon)
print self.export() # View last update data
def export (self):
listsExportItem = []
for index in range(0, self.count()):
stateQIcon = self.itemIcon(index)
state = {
self.CHECK_QICON.name() : True,
self.UNCHECK_QICON.name() : False,
} [stateQIcon.name()]
listsExportItem.append([str(self.itemText(index)), state])
return listsExportItem
myQApplication = QtGui.QApplication([])
myQCustomComboBox = QCustomComboBox()
myQCustomComboBox.show()
sys.exit(myQApplication.exec_())发布于 2014-09-09 01:56:49
最后,我使用文本颜色来区分单击项(也称为选定项)和其他项目。
首先,点击ComboBox打开它的下拉菜单。以下所有单击将选择/取消选中(突出显示红色)项。按“逃逸”关闭拉下。

from PyQt4 import QtCore, QtGui
app = QtGui.QApplication([])
class Combo(QtGui.QComboBox):
def __init__(self, *args, **kwargs):
super(Combo, self).__init__()
for each in ['Item_1','Item_2','Item_3','Item_4','Item_5']:
item=QtGui.QStandardItem(each)
self.model().appendRow(item)
item.setData('black')
self.activated.connect(self.clicked)
self.show()
def clicked(self, clickedNumber=None):
self.blockSignals(True)
self.setCurrentIndex(0)
self.showPopup()
self.view().clearSelection()
clickedItem=self.model().item(clickedNumber)
color=clickedItem.data().toString()
if color=='black': clickedItem.setData('red')
elif color=='red': clickedItem.setData('black')
clickedItem.setForeground(QtGui.QColor(clickedItem.data().toString()))
clickedIndex=self.model().indexFromItem(clickedItem)
self.view().selectionModel().select(clickedIndex, QtGui.QItemSelectionModel.Select)
self.setCurrentIndex(clickedNumber)
self.blockSignals(False)
tree=Combo()
sys.exit(app.exec_())https://stackoverflow.com/questions/25733471
复制相似问题