PyQt: 如何设置下拉框项为可选中?

8 投票
3 回答
23311 浏览
提问于 2025-04-18 00:46

为了让界面上的小工具数量尽量少,我需要找到一种方法,让用户可以选择下拉菜单中的选项,以便过滤出在列表中显示的项目。
假设这个列表中有5个不同的项目类别:“类别 A”、“类别 B”、“类别 C”、“类别 D”、“类别 E”。我可以为每个类别实现单选按钮或复选框。但是这样的话,5个单选按钮或复选框会占用很多界面空间。使用一个可以勾选的下拉框似乎是个不错的选择。你有什么想法吗?

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

3 个回答

-1

(这不是对问题的回答,所以我用了这里的大部分代码)

我添加了一个功能,并把名字改成了 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)
1

这很简单。只需在与下拉框(comboBox)关联的模型中,使用flags()函数将项目设置为可检查(Checkable)。

def flags(self, index):
    return Qt.ItemIsUserCheckable | Qt.ItemIsSelectable | Qt.ItemIsEnabled

更新


这可能不是最好的方法,但应该能解决你的问题。如果你有空间限制,无法显示完整的列表视图(listView),可以尝试调整它的大小,直到它看起来像一个下拉框。

在这里输入图片描述

15

这个多选组合框的想法之前已经有人提过,但我不太确定这是不是最好的解决办法。其实,我们只需要一个带下拉菜单的工具按钮(就像网页浏览器里的历史记录按钮一样)。

这里有一个基本的演示,展示了这两种选项(左边是按钮,右边是组合框):

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

撰写回答