【问题标题】:Get previous and newly selected item on activation in QComboBox在 QComboBox 激活时获取上一个和新选择的项目
【发布时间】:2019-02-15 15:54:39
【问题描述】:

在我的程序中,一些组合框 (QComboBox) 用于进行多项设置。有时不仅需要知道用户选择的项目,还需要知道先前在组合框中选择的项目。 好吧,转移新的选择很容易:

self.MyCombobox.activated[str].connect(self.ComboChange)

但是,当索引更改时,我如何不仅将新选择的项目传递给函数,还将前一个项目传递给函数?

我的折衷解决方案是为每个组合框手动设置一个变量,该变量存储最后选择的值,以便在选择更改时可以访问它。但是考虑到我有很多组合框,这很容易出错,直到可以以不同的方式更新某些框。

提前感谢您的帮助

一个最小的工作示例:

import sys

from PyQt5.QtWidgets import (   QApplication, QWidget, QGridLayout, QComboBox,
                                QLabel)

class BaseWidget(QWidget):
    def __init__(self):
        super(BaseWidget, self).__init__()
        self.setGeometry(300, 300, 300, 200)

        # 2 Labels to display the new and the old item after selection
        self.LabelOldItem = QLabel(self)
        self.LabelNewItem = QLabel(self)

        self.MyCombobox = QComboBox(self)
        self.MyCombobox.addItems(['Item 1', 'Item 2', 'Item 3', 'Item 4'])
        self.MyCombobox.activated[str].connect(self.ComboChange)


        grid = QGridLayout()
        grid.addWidget(self.MyCombobox, 0, 0, 1, 1)
        grid.addWidget(self.LabelOldItem, 1, 0, 1, 1)
        grid.addWidget(self.LabelNewItem, 2, 0, 1, 1)

        self.setLayout(grid)

    def ComboChange(self, newitem):
        self.LabelOldItem.setText('Previous Selection: ') # <- crucial point
        # How can i transfer both, not only the new item but also the previous
        # item of the combobox when it gets activated?
        self.LabelNewItem.setText('New Selection: <b>' + newitem + '</b>')


if __name__ == '__main__':

    app = QApplication(sys.argv)

    pyqtComboExample = BaseWidget()
    pyqtComboExample.show()
    sys.exit(app.exec_())

【问题讨论】:

    标签: python pyqt pyqt5 qcombobox


    【解决方案1】:

    一种可能的解决方案是创建一个自定义 QComboBox:

    import sys
    from PyQt5 import QtCore, QtWidgets
    
    
    class ComboBox(QtWidgets.QComboBox):
        new_signal = QtCore.pyqtSignal(str, str)
    
        def __init__(self, parent=None):
            super(ComboBox, self).__init__(parent)
            self.lastSelected = ""
            self.activated[str].connect(self.onActivated)
    
        def onActivated(self, text):
            self.new_signal.emit(self.lastSelected, text)
            self.lastSelected = text
    
    
    class BaseWidget(QtWidgets.QWidget):
        def __init__(self):
            super(BaseWidget, self).__init__()
            self.setGeometry(300, 300, 300, 200)
    
            # 2 Labels to display the new and the old item after selection
            self.LabelOldItem = QtWidgets.QLabel()
            self.LabelNewItem = QtWidgets.QLabel()
    
            self.MyCombobox = ComboBox()
            self.MyCombobox.addItems(['Item 1', 'Item 2', 'Item 3', 'Item 4'])
            self.MyCombobox.new_signal.connect(self.ComboChange)
    
            grid = QtWidgets.QGridLayout(self)
            grid.addWidget(self.MyCombobox, 0, 0, 1, 1)
            grid.addWidget(self.LabelOldItem, 1, 0, 1, 1)
            grid.addWidget(self.LabelNewItem, 2, 0, 1, 1)
    
        def ComboChange(self, lastitem, newitem):
            self.LabelOldItem.setText('Previous Selection: <b>{}</b>'.format(lastitem))
            self.LabelNewItem.setText('New Selection: <b>{}</b>'.format(newitem))
    
    
    if __name__ == '__main__':
    
        app = QtWidgets.QApplication(sys.argv)
        pyqtComboExample = BaseWidget()
        pyqtComboExample.show()
        sys.exit(app.exec_())
    

    另一种可能的解决方案是使用sender() 获取使用的QComboBox,并将旧项目保存在属性中:

    import sys
    from PyQt5 import QtCore, QtWidgets
    
    
    class BaseWidget(QtWidgets.QWidget):
        def __init__(self):
            super(BaseWidget, self).__init__()
            self.setGeometry(300, 300, 300, 200)
    
            # 2 Labels to display the new and the old item after selection
            self.LabelOldItem = QtWidgets.QLabel()
            self.LabelNewItem = QtWidgets.QLabel()
    
            self.MyCombobox = QtWidgets.QComboBox()
            self.MyCombobox.addItems(['Item 1', 'Item 2', 'Item 3', 'Item 4'])
            self.MyCombobox.activated[str].connect(self.ComboChange)
    
            grid = QtWidgets.QGridLayout(self)
            grid.addWidget(self.MyCombobox, 0, 0, 1, 1)
            grid.addWidget(self.LabelOldItem, 1, 0, 1, 1)
            grid.addWidget(self.LabelNewItem, 2, 0, 1, 1)
    
        def ComboChange(self, newitem):
            combo = self.sender()
            lastitem = combo.property("lastitem")
            self.LabelOldItem.setText('Previous Selection: <b>{}</b>'.format(lastitem))
            self.LabelNewItem.setText('New Selection: <b>{}</b>'.format(newitem))
            combo.setProperty("lastitem", newitem)
    
    
    if __name__ == '__main__':
    
        app = QtWidgets.QApplication(sys.argv)
        pyqtComboExample = BaseWidget()
        pyqtComboExample.show()
        sys.exit(app.exec_())
    

    【讨论】:

    • 正是我想要的。创建一个自定义的 QComboBox 是我之前已经考虑过的一件事,但不知何故我没有设法让它工作。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-20
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    • 1970-01-01
    相关资源
    最近更新 更多