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:

  1. First, we import the GUI library:
            from PyQt4.QtGui import * 
    
  2. Now, we create our combobox object:
            cb = QComboBox() 
    
  3. Next, we add the items that we want the user to choose from:
            cb.addItems(["North", "South", "West", "East"]) 
    
  4. Then, we resize the widget: ...

Get QGIS Python Programming Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.