为了将 GUI 小部件数量保持在最低限度,我需要找到一种方法来为用户提供下拉菜单项的选择,这些菜单项可用于过滤掉列表中显示的小部件项。 假设 listWidget 列出了 5 个不同类别的项目:“Cat A”、“Cat B”、“Cat C”、“Cat D”、“Cat E”。我可以为每个项目类别实现单选或复选框。但是 5 个单选按钮或复选框会占用大量 GUI 空间。带有可检查项目的组合框似乎是一个正确的选择。有什么想法吗?
from PyQt4 import QtGui, QtCore
import sys, os
class CheckableComboBox(QtGui.QComboBox):
def __init__(self):
super(CheckableComboBox, self).__init__()
def flags(self, index):
return Qt.ItemIsUserCheckable | Qt.ItemIsSelectable | Qt.ItemIsEnabled
class Dialog_01(QtGui.QMainWindow):
def __init__(self):
super(QtGui.QMainWindow,self).__init__()
myQWidget = QtGui.QWidget()
myBoxLayout = QtGui.QVBoxLayout()
myQWidget.setLayout(myBoxLayout)
self.setCentralWidget(myQWidget)
self.ComboBox = CheckableComboBox()
for i in range(3):
self.ComboBox.addItem("Combobox Item " + str(i))
myBoxLayout.addWidget(self.ComboBox)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
dialog_1 = Dialog_01()
dialog_1.show()
dialog_1.resize(480,320)
sys.exit(app.exec_())
多选组合的想法已经出现过,但我不确定它是最好的解决方案。实际上,所需要的只是一个带有下拉菜单的工具按钮(类似于网络浏览器中的历史记录按钮)。
这是一个基本演示,说明了这两个选项(左侧按钮,右侧组合):
PyQt5:
from PyQt5 import QtCore, QtGui, QtWidgets
class CheckableComboBox(QtWidgets.QComboBox):
def __init__(self, title = '', parent=None):
super().__init__(parent)
self.setTitle(title)
self.view().pressed.connect(self.handleItemPressed)
self.setModel(QtGui.QStandardItemModel(self))
def handleItemPressed(self, index):
item = self.model().itemFromIndex(index)
if item.checkState() == QtCore.Qt.Checked:
item.setCheckState(QtCore.Qt.Unchecked)
else:
item.setCheckState(QtCore.Qt.Checked)
def title(self):
return self._title
def setTitle(self, title):
self._title = title
self.repaint()
def paintEvent(self, event):
painter = QtWidgets.QStylePainter(self)
painter.setPen(self.palette().color(QtGui.QPalette.Text))
opt = QtWidgets.QStyleOptionComboBox()
self.initStyleOption(opt)
opt.currentText = self._title
painter.drawComplexControl(QtWidgets.QStyle.CC_ComboBox, opt)
painter.drawControl(QtWidgets.QStyle.CE_ComboBoxLabel, opt)
class Dialog(QtWidgets.QWidget):
def __init__(self):
super().__init__()
hbox = QtWidgets.QHBoxLayout()
self.comboBox = CheckableComboBox('Categories', self)
self.toolButton = QtWidgets.QToolButton(self)
self.toolButton.setText('Categories ')
self.toolMenu = QtWidgets.QMenu(self)
for index in range(3):
self.comboBox.addItem(f'Category {index}')
item = self.comboBox.model().item(index, 0)
item.setCheckState(QtCore.Qt.Unchecked)
action = self.toolMenu.addAction(f'Category {index}')
action.setCheckable(True)
self.toolButton.setMenu(self.toolMenu)
self.toolButton.setPopupMode(QtWidgets.QToolButton.InstantPopup)
hbox.addWidget(self.toolButton)
hbox.addWidget(self.comboBox)
layout = QtWidgets.QVBoxLayout(self)
layout.addLayout(hbox)
layout.addStretch()
if __name__ == '__main__':
app = QtWidgets.QApplication(['Test'])
dialog = Dialog()
dialog.show()
app.exec()
PyQt4:
from PyQt4 import QtCore, QtGui
class CheckableComboBox(QtGui.QComboBox):
def __init__(self, title = '', parent=None):
super().__init__(parent)
self.setTitle(title)
self.view().pressed.connect(self.handleItemPressed)
self.setModel(QtGui.QStandardItemModel(self))
def handleItemPressed(self, index):
item = self.model().itemFromIndex(index)
if item.checkState() == QtCore.Qt.Checked:
item.setCheckState(QtCore.Qt.Unchecked)
else:
item.setCheckState(QtCore.Qt.Checked)
def title(self):
return self._title
def setTitle(self, title):
self._title = title
self.repaint()
def paintEvent(self, event):
painter = QtGui.QStylePainter(self)
painter.setPen(self.palette().color(QtGui.QPalette.Text))
opt = QtGui.QStyleOptionComboBox()
self.initStyleOption(opt)
opt.currentText = self._title
painter.drawComplexControl(QtGui.QStyle.CC_ComboBox, opt)
painter.drawControl(QtGui.QStyle.CE_ComboBoxLabel, opt)
class Dialog(QtGui.QWidget):
def __init__(self):
super().__init__()
hbox = QtGui.QHBoxLayout()
self.comboBox = CheckableComboBox('Categories', self)
self.toolButton = QtGui.QToolButton(self)
self.toolButton.setText('Categories ')
self.toolMenu = QtGui.QMenu(self)
for index in range(3):
self.comboBox.addItem('Category %s' % index)
item = self.comboBox.model().item(index, 0)
item.setCheckState(QtCore.Qt.Unchecked)
action = self.toolMenu.addAction('Category %s' % index)
action.setCheckable(True)
self.toolButton.setMenu(self.toolMenu)
self.toolButton.setPopupMode(QtGui.QToolButton.InstantPopup)
hbox.addWidget(self.toolButton)
hbox.addWidget(self.comboBox)
layout = QtGui.QVBoxLayout(self)
layout.addLayout(hbox)
layout.addStretch()
if __name__ == '__main__':
app = QtGui.QApplication(['Test'])
dialog = Dialog()
dialog.show()
app.exec()
这很容易。只需使用与组合框关联的模型中的 flags() 函数设置项目可检查。
def flags(self, index):
return Qt.ItemIsUserCheckable | Qt.ItemIsSelectable | Qt.ItemIsEnabled
更新
这可能不是最好的方法,但它应该可以解决您的问题。这是你可能想要看的东西,如果你有空间限制并且无法显示完整的列表视图,只需调整它的大小直到它看起来像一个组合框。
(不是问题的答案,因此我使用了这里的大部分代码)
如果其他人想要有一个 RadioComboBox 类,我添加了一个函数并将名称更改为 RadioComboBox。
from PyQt4 import QtCore
from PyQt4.QtGui import QComboBox, QStandardItemModel
class RadioComboBox(QComboBox):
def __init__(self):
super(RadioComboBox, self).__init__()
self.view().pressed.connect(self.handle_item_pressed)
self.setModel(QStandardItemModel(self))
def handle_item_pressed(self, index):
item = self.model().itemFromIndex(index)
target_row = item.index().row()
if item.checkState() != QtCore.Qt.Checked:
item.setCheckState(QtCore.Qt.Checked)
self.check_others(target_row)
def check_others(self, target_row):
for i in range(self.model().rowCount()):
if i == target_row:
continue
else:
item = self.model().item(i)
item.setCheckState(QtCore.Qt.Unchecked)