- PyQt5 - Home
- PyQt5 - Introduction
- PyQt5 - What’s New
- PyQt5 - Hello World
- PyQt5 - Major Classes
- PyQt5 - Using Qt Designer
- PyQt5 - Signals & Slots
- PyQt5 - Layout Management
- PyQt5 - Basic Widgets
- PyQt5 - QDialog Class
- PyQt5 - QMessageBox
- PyQt5 - Multiple Document Interface
- PyQt5 - Drag & Drop
- PyQt5 - Database Handling
- PyQt5 - Drawing API
- PyQt5 - BrushStyle Constants
- PyQt5 - QClipboard
- PyQt5 - QPixmap Class
- PyQt5 Useful Resources
- PyQt5 - Quick Guide
- PyQt5 - Useful Resources
- PyQt5 - Discussion
PyQt5 - QComboBox Widget
A QComboBox object presents a dropdown list of items to select from. It takes minimum screen space on the form required to display only the currently selected item.
A Combo box can be set to be editable; it can also store pixmap objects. The following methods are commonly used −
| Sr.No. | Methods & Description |
|---|---|
| 1 |
addItem() Adds string to collection |
| 2 |
addItems() Adds items in a list object |
| 3 |
Clear() Deletes all items in the collection |
| 4 |
count() Retrieves number of items in the collection |
| 5 |
currentText() Retrieves the text of currently selected item |
| 6 |
itemText() Displays text belonging to specific index |
| 7 |
currentIndex() Returns index of selected item |
| 8 |
setItemText() Changes text of specified index |
QComboBox Signals
The following methods are commonly used in QComboBox Signals −
| Sr.No. | Methods & Description |
|---|---|
| 1 |
activated() When the user chooses an item |
| 2 |
currentIndexChanged() Whenever the current index is changed either by the user or programmatically |
| 3 |
highlighted() When an item in the list is highlighted |
Example
Let us see how some features of QComboBox widget are implemented in the following example.
Items are added in the collection individually by addItem() method or items in a List object by addItems() method.
self.cb.addItem("C++")
self.cb.addItems(["Java", "C#", "Python"])
QComboBox object emits currentIndexChanged() signal. It is connected to selectionchange() method.
Items in a combo box are listed using itemText() method for each item. Label belonging to the currently chosen item is accessed by currentText() method.
def selectionchange(self,i):
print "Items in the list are :"
for count in range(self.cb.count()):
print self.cb.itemText(count)
print "Current index",i,"selection changed ",self.cb.currentText()
The entire code is as follows −
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class combodemo(QWidget):
def __init__(self, parent = None):
super(combodemo, self).__init__(parent)
layout = QHBoxLayout()
self.cb = QComboBox()
self.cb.addItem("C")
self.cb.addItem("C++")
self.cb.addItems(["Java", "C#", "Python"])
self.cb.currentIndexChanged.connect(self.selectionchange)
layout.addWidget(self.cb)
self.setLayout(layout)
self.setWindowTitle("combo box demo")
def selectionchange(self,i):
print "Items in the list are :"
for count in range(self.cb.count()):
print self.cb.itemText(count)
print "Current index",i,"selection changed ",self.cb.currentText()
def main():
app = QApplication(sys.argv)
ex = combodemo()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Output
The above code produces the following output −
Items in the list are −
C C++ Java C# Python Current selection index 4 selection changed Python