在 QT Python 中的下拉列表中进行多项选择

问题描述 投票:0回答:1

我正在使用 Python 和 Qt 6 开发 GUI。当前版本允许我从数据框中仅选择一列。但是,我想实现一个下拉列表,使我能够将多个列绘制在一起。

如何在 PyQt6 中创建下拉列表并使用它从数据框中选择多个列进行绘图?任何代码示例或建议将不胜感激。谢谢!

我正在尝试创建复选框来选择数据框中的多个列,以便在 PyQt6 GUI 中进行绘图。但是,我注意到这种方法会消耗大量空间并对性能产生负面影响。

matplotlib pyqt
1个回答
0
投票

尝试一下,看看它是否满足您的需求。您可以随意修改它,只要您认为合适。 下面的解决方案受到以下 QT5 解决方案的启发。 https://gis.stackexchange.com/questions/350148/qcombobox-multiple-selection-pyqt5

multicombobox.py

from PyQt6.QtGui import QStandardItemModel, QStandardItem
from PyQt6.QtWidgets import QComboBox
from PyQt6.QtCore import Qt


class MultiComboBox(QComboBox):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setEditable(True)
        self.lineEdit().setReadOnly(True)
        self.setModel(QStandardItemModel(self))

        # Connect to the dataChanged signal to update the text
        self.model().dataChanged.connect(self.updateText)

    def addItem(self, text, data=None):
        item = QStandardItem()
        item.setText(text)
        item.setFlags(Qt.ItemFlag.ItemIsEnabled | Qt.ItemFlag.ItemIsUserCheckable)
        item.setData(Qt.CheckState.Unchecked, Qt.ItemDataRole.CheckStateRole)
        self.model().appendRow(item)

    def addItems(self, texts):
        for text in texts:
            self.addItem(text)

    def updateText(self):
        selected_items = [self.model().item(i).text() for i in range(self.model().rowCount())
                          if self.model().item(i).checkState() == Qt.CheckState.Checked]
        self.lineEdit().setText(", ".join(selected_items))

    def showPopup(self):
        super().showPopup()
        # Set the state of each item in the dropdown
        for i in range(self.model().rowCount()):
            item = self.model().item(i)
            combo_box_view = self.view()
            combo_box_view.setRowHidden(i, False)
            check_box = combo_box_view.indexWidget(item.index())
            if check_box:
                check_box.setChecked(item.checkState() == Qt.CheckState.Checked)

    def hidePopup(self):
        # Update the check state of each item based on the checkbox state
        for i in range(self.model().rowCount()):
            item = self.model().item(i)
            combo_box_view = self.view()
            check_box = combo_box_view.indexWidget(item.index())
            if check_box:
                item.setCheckState(Qt.CheckState.Checked if check_box.isChecked() else Qt.CheckState.Unchecked)
        super().hidePopup()

multicombobox_test_window.py

from PyQt6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QPushButton, QWidget
from multicombobox import MultiComboBox
import sys


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("MultiComboBox Test")

        # Create MultiComboBox instance
        self.multiComboBox = MultiComboBox()
        self.multiComboBox.addItems(["Option 1", "Option 2", "Option 3", "Option 4"])

        # Create a button to show selected items
        self.showSelectionButton = QPushButton("Show Selected Items")
        self.showSelectionButton.clicked.connect(self.showSelection)

        # Layout
        layout = QVBoxLayout()
        layout.addWidget(self.multiComboBox)
        layout.addWidget(self.showSelectionButton)

        # Set the central widget
        centralWidget = QWidget()
        centralWidget.setLayout(layout)
        self.setCentralWidget(centralWidget)

    def showSelection(self):
        selectedItems = self.multiComboBox.lineEdit().text()
        print("Selected items:", selectedItems)

def main():
    app = QApplication(sys.argv)
    mainWindow = MainWindow()
    mainWindow.show()
    sys.exit(app.exec())

if __name__ == "__main__":
    main()
© www.soinside.com 2019 - 2024. All rights reserved.