- PyQt Tutorial
- PyQt - Home
- PyQt - Introduction
- PyQt - Hello World
- PyQt - Major Classes
- PyQt - Using Qt Designer
- PyQt - Signals and Slots
- PyQt - Layout Management
- PyQt - Basic Widgets
- PyQt - QDialog Class
- PyQt - QMessageBox
- PyQt - Multiple Document Interface
- PyQt - Drag and Drop
- PyQt - Database Handling
- PyQt - Drawing API
- PyQt - BrushStyle Constants
- PyQt - QClipboard
- PyQt - QPixmap Class
- PyQt Useful Resources
- PyQt - Quick Guide
- PyQt - Useful Resources
- PyQt - Discussion
PyQt - 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 −
Given below are the most commonly used methods of QComboBox.
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
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 PyQt4.QtCore import * from PyQt4.QtGui 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()
The above code produces the following output −
Items in the list are −
C C++ Java C# Python Current selection index 4 selection changed Python