SoFunction
Updated on 2024-11-16

python GUI library graphical interface development PyQt5 drop-down list box control QComboBox detailed usage and examples

Introducing QComboBox, a PyQt5 drop-down list box control.

QComboBox is a control that combines a button and a drop-down option, also known as a drop-down list box.

The commonly used methods in the QComboBox class are listed in the table

methodologies descriptive
addItem() Add a drop-down option
addItems() Adding drop-down options from a list
Clear() Remove all options from the drop-down collection
count() Returns the number of drop-down options in the collection
currentText() Returns the text of the selected option
itemText(i) Get the option text of the item with index i
currentIndex() Returns the index of the selected item
setItemText(int index,text) Change the text of the serial number to index

Common signals in the QComboBox class

code hidden meaning
Activated This signal is emitted when the user selects a drop-down option
currentIndexChanged This signal is emitted when the index of a drop-down option changes
highlighted Emits this signal when an already selected drop-down option is selected

Example of using QComboBox button for drop-down list box control

import sys
from  import *
from  import *
from  import *

class ComboxDemo(QWidget):
  def __init__(self,parent=None):
    super(ComboxDemo, self).__init__(parent)
    #Set Title
    ('ComBox example')
    # Set the initial interface size
    (300,90)

    #Vertical Layout
    layout=QVBoxLayout()
    #create labels, default blank
    self.btn1=QLabel('')

    # Instantiate the QComBox object
    =QComboBox()
    # Add entries individually
    ('C')
    ('C++')
    ('Python')
    #Multiple add entries
    (['Java','C#','PHP'])
    # Emit a signal to trigger a bound event when the dropdown index changes
    ()

    # Controls are added to the layout to set up the layout
    ()
    (self.btn1)
    (layout)

  def selectionchange(self,i):
    The # tag is used to display the selected text
    #currentText(): return the text of the selected option
    self.(())
    print('Items in the list are:')
    # Output the index and corresponding content of each option in the set of options
    #count(): return the number in the set of options
    for count in range(()):
      print('Item'+str(count)+'='+(count))
      print('current index',i,'selection changed',())

if __name__ == '__main__':
  app=QApplication()
  comboxDemo=ComboxDemo()
  ()
  (app.exec_())

The rendering is as follows

Code analysis of the drop-down list box control QComboBox:

This example shows a drop-down list box and a label, where there are several options in the drop-down list box, and either a single option can be added using QCombobox's addItem() method, or multiple options can be added using the addItems() method: the label displays the options that are selected from the drop-down list box

    # Add entries individually
    ('C')
    ('C++')
    ('Python')
    #Multiple add entries
    (['Java','C#','PHP'])

The currentIndexChanged signal will be emitted when the selected option of the drop-down list box changes, linking to the custom slot function selectionChange()

()

In the method, when an option in the drop-down list box is selected, the option text will be set to the text of the label, and the label will be resized

  def selectionchange(self,i):
    The # tag is used to display the selected text
    #currentText(): return the text of the selected option
    self.(())

This article describes the PyQt5 drop-down list box control QComboBox detailed use of methods and examples, more about the PyQt5 drop-down list box control QComboBox knowledge, please see the following related links