How to get the model of ComboBox in PyQt5

In this tutorial, we will see how to get the model of a ComboBox in PyQt5. We need to import the PyQt5 library and some of the modules like QtWidgets, QtGui, QtCore associated with the PyQt5 library. Firstly we need to install the PyQt5 library in our system.

Installation of PyQt5

To install this particular library we first need to open our Command Prompt (Windows Users) or Terminal (Mac Users) and type in this following command.

pip install PyQt5

Importing the libraries

from PyQt5.QtWidgets import * 
from PyQt5 import QtGui 
from PyQt5.QtGui import *  
import sys

Creating the Window of the ComboBox Model

We define a class named Window and it takes QMainWindow as the parameter of the class. Inside this class, we define two functions. The first function creates the main window and the second function sets the attributes of the UI (User Interface) components. To create the window you have to type the following block of Python code:

class Windo_ComboBox(QMainWindow):
    def __init__(self): 
        super().__init__() 
        self.setWindowTitle("Python ComboBox Model") 
        self.setGeometry(100, 100, 600, 400) 
        self.UI()
        self.show() 

In the above code, we set the title and size/geometry of the Main window. We also initiate the UI function which will contain the attributes of the ComboBox Model and now we will define the UI function:

def UI(self): 
    self.combo_box = QComboBox(self) 
    self.combo_box.setGeometry(225, 150, 150, 30) 
    sports_list = ["Football", "Cricket", "Basketball", "Golf"] 
    self.combo_box.setEditable(True) 
    self.combo_box.addItems(sports_list) 
    combobox_model = QtGui.QStandardItemModel(0, 1) 
    add_item = QtGui.QStandardItem("Add model")  
    combobox_model.appendRow(add_item) 
    self.combo_box.setModel(combobox_model) 
    get_modelname = self.combo_box.model() 
    label = QLabel("Model = " + str(get_modelname), self) 
    label.setGeometry(100, 100, 800, 30)

Some of the important points to note for the above function are:

  • We first create an instance of the object ‘QComboBox’ and set its geometry.
  • Then we create a general list so that we can add this list to the model we have just created.
  • To get the model name we use the syntax ‘combo_box.model()’ and this argument does not take any parameter.

The final three lines of code create our PyQt5 app, create an instance of our main window, and start the execution of our app.

App = QApplication(sys.argv) 
window = Window_ComboBox()  
sys.exit(App.exec())

The entire code is attached below:

from PyQt5.QtWidgets import * 
from PyQt5 import QtGui 
from PyQt5.QtGui import *  
import sys

class Window_ComboBox(QMainWindow):
    def UI(self): 
        self.combo_box = QComboBox(self) 
        self.combo_box.setGeometry(225, 150, 150, 30) 
        sports_list = ["Football", "Cricket", "Basketball", "Golf"] 
        self.combo_box.setEditable(True) 
        self.combo_box.addItems(sports_list) 
        combobox_model = QtGui.QStandardItemModel(0, 1) 
        add_item = QtGui.QStandardItem("Add model") 
        combobox_model.appendRow(add_item) 
        self.combo_box.setModel(combobox_model) 
        get_modelname = self.combo_box.model() 
        label = QLabel("Model = " + str(get_modelname), self)  
        label.setGeometry(100, 100, 800, 30) 
  
    def __init__(self): 
        super().__init__()   
        self.setWindowTitle("Python ComboBox Model") 
        self.setGeometry(100, 100, 600, 400)
        self.UI()
        self.show() 
 
App = QApplication(sys.argv) 
window = Window_ComboBox()  
sys.exit(App.exec())

The above code when executed will open a new window/dialog box like this:

ComboBox Model Output

If you want to learn more about PyQt5, please refer to the links attached below

Create a Simple Login Form using PyQt5 in Python
Digital stopwatch GUI Application in Python – PyQt5
Displaying an image using PyQt5 in Python

Leave a Reply

Your email address will not be published. Required fields are marked *