3

Using signal and slots method to get index and text of selected items in QcomboBox is well known. The code below is an illustration of that. But how can I tell my code to recieve and send the first display item at startup, Methods like Activated, currentIndexChanged, Highlighted only work by dropdown QCombobox.

enter image description here

Output:

1
item2
2
item3

The sample code:

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_())
coffeewin
  • 182
  • 3
  • 6
  • 26
Pavel.D
  • 561
  • 1
  • 15
  • 41

2 Answers2

0

You could make your own custom signal and emit that in the __init__ section of your object. This signal could then access the first item in the QComboBox. Another method could be to use a singleShot timer but making a custom signal would probably be better. You can retrieve the current index with currentIndex() or 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_())
nathancy
  • 42,661
  • 14
  • 115
  • 137
0

Try it:

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_())
S. Nick
  • 12,879
  • 8
  • 25
  • 33