为了将 GUI 小部件数量保持在最低限度,我需要找到一种方法来为用户提供下拉菜单项的选择,这些菜单项可用于过滤掉 listWidget 项中显示的内容。
假设 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_())
这种多选组合的想法之前出现 https://stackoverflow.com/q/21186779/984421,但我不确定这是最好的解决方案。实际上,所需要的只是一个带有下拉菜单的工具按钮(类似于网络浏览器中的历史记录按钮)。
这是一个基本演示,说明了这两个选项(左侧按钮,右侧组合):
PyQt5:
from PyQt5 import QtWidgets, QtGui, QtCore
class CheckableComboBox(QtWidgets.QComboBox):
def __init__(self):
super(CheckableComboBox, self).__init__()
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)
class Dialog_01(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
myQWidget = QtWidgets.QWidget()
myBoxLayout = QtWidgets.QHBoxLayout()
myQWidget.setLayout(myBoxLayout)
self.setCentralWidget(myQWidget)
self.ComboBox = CheckableComboBox()
self.toolbutton = QtWidgets.QToolButton(self)
self.toolbutton.setText('Categories ')
self.toolmenu = QtWidgets.QMenu(self)
for i in range(3):
self.ComboBox.addItem('Category %s' % i)
item = self.ComboBox.model().item(i, 0)
item.setCheckState(QtCore.Qt.Unchecked)
action = self.toolmenu.addAction('Category %s' % i)
action.setCheckable(True)
self.toolbutton.setMenu(self.toolmenu)
self.toolbutton.setPopupMode(QtWidgets.QToolButton.InstantPopup)
myBoxLayout.addWidget(self.toolbutton)
myBoxLayout.addWidget(self.ComboBox)
if __name__ == '__main__':
app = QtWidgets.QApplication(['Test'])
dialog_1 = Dialog_01()
dialog_1.show()
app.exec_()
**PyQt4**:
from PyQt4 import QtGui, QtCore
class CheckableComboBox(QtGui.QComboBox):
def __init__(self):
super(CheckableComboBox, self).__init__()
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)
class Dialog_01(QtGui.QMainWindow):
def __init__(self):
super(QtGui.QMainWindow, self).__init__()
myQWidget = QtGui.QWidget()
myBoxLayout = QtGui.QHBoxLayout()
myQWidget.setLayout(myBoxLayout)
self.setCentralWidget(myQWidget)
self.ComboBox = CheckableComboBox()
self.toolbutton = QtGui.QToolButton(self)
self.toolbutton.setText('Categories ')
self.toolmenu = QtGui.QMenu(self)
self.toolbutton.setMenu(self.toolmenu)
self.toolbutton.setPopupMode(QtGui.QToolButton.InstantPopup)
for i in range(3):
self.ComboBox.addItem('Category %s' % i)
item = self.ComboBox.model().item(i, 0)
item.setCheckState(QtCore.Qt.Unchecked)
action = self.toolmenu.addAction('Category %s' % i)
action.setCheckable(True)
myBoxLayout.addWidget(self.toolbutton)
myBoxLayout.addWidget(self.ComboBox)
if __name__ == '__main__':
app = QtGui.QApplication(['Test'])
dialog_1 = Dialog_01()
dialog_1.show()
app.exec_()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)