在启动时发送 QComboBox 中项目的当前显示索引

Sending the current displayed index of item in QComboBox at start up

使用signal and slots 方法获取QcomboBox 中所选项目的索引和文本是众所周知的。下面的代码对此进行了说明。但是我怎么能告诉我的代码在启动时接收和发送第一个显示项目,像 Activated, currentIndexChanged, Highlighted 这样的方法只能通过下拉列表 QCombobox 工作。

输出:

1
item2
2
item3

示例代码:

from PyQt5 import QtCore, QtWidgets, QtGui

class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent)
        self.setLayout(QtWidgets.QVBoxLayout())
        combo = QtWidgets.QComboBox(self)
        self.layout().addWidget(combo)
        combo.addItems(["item1", "item2", "item3"])
        combo.setMinimumWidth(150)
        combo.activated[int].connect(self.onActivatedIndex)
        combo.activated[str].connect(self.onActivatedText)

    @QtCore.pyqtSlot(int)
    def onActivatedIndex(self, index):
        print(index)

    @QtCore.pyqtSlot(str)
    def onActivatedText(self, text):
        print(text)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

您可以创建自己的自定义信号并在对象的 __init__ 部分发出该信号。然后,此信号可以访问 QComboBox 中的第一项。另一种方法可能是使用 singleShot 计时器,但制作自定义信号可能会更好。您可以使用 currentIndex()currentText().

检索当前索引
from PyQt5 import QtCore, QtWidgets, QtGui

class Widget(QtWidgets.QWidget):
    startup = QtCore.pyqtSignal()

    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent)
        self.setLayout(QtWidgets.QVBoxLayout())
        self.combo = QtWidgets.QComboBox(self)
        self.layout().addWidget(self.combo)
        self.combo.addItems(["item1", "item2", "item3"])
        self.combo.setMinimumWidth(150)
        self.combo.activated[int].connect(self.onActivatedIndex)
        self.combo.activated[str].connect(self.onActivatedText)

        self.startup.connect(self.current_index)
        self.startup.emit()

    @QtCore.pyqtSlot()
    def current_index(self):
        print(self.combo.currentIndex())
        print(self.combo.currentText())

    @QtCore.pyqtSlot(int)
    def onActivatedIndex(self, index):
        print(index)

    @QtCore.pyqtSlot(str)
    def onActivatedText(self, text):
        print(text)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

试一试:

from PyQt5 import QtCore, QtWidgets, QtGui

class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent)
        self.setLayout(QtWidgets.QVBoxLayout())
        self.combo = QtWidgets.QComboBox(self)
        self.layout().addWidget(self.combo)
        self.combo.addItems(["item1", "item2", "item3"])
        self.combo.setMinimumWidth(150)

        self.combo.activated[int].connect(self.onActivatedIndex)
        self.combo.activated[str].connect(self.onActivatedText)

        self.combo.activated.emit(1)                           # <---

    @QtCore.pyqtSlot(int)
    def onActivatedIndex(self, index):
        print("\nindex->", index)
        print("text ->", self.combo.itemText(index))

    @QtCore.pyqtSlot(str)
    def onActivatedText(self, text):
        print(text)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())