Creating a combobox
A combobox provides a drop-down list to limit the user's selection to a defined set of choices. In this recipe, we'll create a simple combobox.
Getting ready
Open the QGIS Python Console by selecting the Plugins menu and then clicking on Python Console.
How to do it...
In this recipe, we will initialize the combobox widget, add choices to it, resize it, display it, and then capture the user input in a variable for printing to the console. To do this, we need to perform the following steps:
- Frist, we import the GUI library:
from PyQt4.QtGui import *
- Now, we create our combobox object:
cb = QComboBox()
- Next, we add the items that we want the user to choose from:
cb.addItems(["North", "South", "West", "East"])
- Then, we resize the widget:
cb.resize(200,35)
- Now we can display the widget to the user:
cb.show()
- Next, we need to select an item from the list.
- Now, we set the user's choice to a variable:
text = cb.currentText()
- Finally...