gpt4 book ai didi

python - PyQt:如何将组合框项目设置为可检查?

转载 作者:太空狗 更新时间:2023-10-30 02:20:30 26 4
gpt4 key购买 nike

为了将 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_())

最佳答案

这个多选组合的想法有come up before ,但我不确定它是否是最佳解决方案。实际上,所需要的只是一个带有下拉菜单的工具按钮(类似于网络浏览器中的历史记录按钮)。

这是一个基本演示,说明了这两个选项(左侧按钮,右侧组合):

screenshot screenshot

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_()

关于python - PyQt:如何将组合框项目设置为可检查?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22775095/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com