PyQt5

Using a QComboBox


main index



Using a QComboBox



Figure 1


Listing 1 (combo_box_ui.py)


# A demo of how to get notifications that a item has been selected
# in a QComboBox.
  
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
  
import sys
  
class Demo(QDialog):
    def __init__(self, parent=None):
        QDialog.__init__(self, parent)
  
        self.setWindowFlags(Qt.WindowStaysOnTopHint)
        self.setWindowTitle("Demo QComboBox")
        self.setFixedSize(400, 200)
        
        # Create the main vertical layout.
        vlayout = QVBoxLayout()
        self.setLayout(vlayout)    
        
        self.addComboPanel(vlayout)
        
        # Ensure the comboPanel is pushed to the upper edge of the
        # Dialog window.
        vlayout.addStretch()    
        
        self.show()
    #--------------------------------------------------------------------
    def addComboPanel(self, parentLayout):
        combo = QComboBox(self)
        combo.addItem("High Quality")    # index = 0
        combo.addItem("Medium Quality") # index = 1
        combo.addItem("Low Quality")    # index = 2
        combo.activated[int].connect(self.comboIntAction)
        combo.activated[str].connect(self.comboStrAction)
        parentLayout.addWidget(combo)
        
    #--------------------------------------------------------------------        
    # Activated by user selecting an item in the QComboBox.
    def comboIntAction(self, index):
        print('The index of selected combo item is %d' % index)
    #--------------------------------------------------------------------
    # Activated by user selecting an item in the QComboBox.
    def comboStrAction(self, text):
        print('The label of selected combo item is "%s"' % text)
    #--------------------------------------------------------------------    
# ========================================================                
if __name__ == '__main__':
    # Create the Qt Application
    app = QApplication(sys.argv)
    demo = Demo() # <<-- Create an instance
    demo.show()
    sys.exit(app.exec_())



© 2002- Malcolm Kesson. All rights reserved.