[EN]Developing a QGIS Plugin: Retrieving User Input


When you first start developing QGIS plugins, it is easy to assume that a button should automatically “do something” when it is clicked.

In reality, a button does nothing at all—until you tell it what to do.

In this article, we will learn how to retrieve values entered by users in a QGIS plugin interface, understand where those values are stored, and demystify what really happens when a button is clicked.

The goal is not to write a lot of code, but to understand the connection between the user interface and the plugin logic, without unnecessary jargon.



Retrieving and Using User Input

In the previous articles, we created a minimal QGIS plugin, added a button to the interface, and displayed a dialog window when the user clicked it.

That was an important milestone.

However, our plugin still does not do anything useful.

In this article, we will take the next essential step: retrieving values entered by the user and making them available to our Python code.

But first…

When people begin developing QGIS plugins, their first encounter with Python code can be intimidating.

Multiple files, classes, methods, and unfamiliar keywords can make the whole process seem more complicated than it really is.

The good news is that you do not need to understand everything immediately.

Reading code is a skill that develops gradually.

If you have not already done so, you may find it useful to read:

How to Read QGIS Plugin Code Without Panicking

That article introduces a simple and reassuring approach to understanding plugin code without feeling overwhelmed.


Why a Button Does Not “Do Anything” by Itself

A fundamental concept is worth understanding from the beginning:

A Qt dialog does not perform any action on its own.

Its purpose is simply to:

  • display input fields,
  • allow users to enter or select values,
  • present information through a graphical interface.

For example, a dialog may contain:

  • text boxes,
  • drop-down lists,
  • check boxes,
  • numeric fields.

If your plugin never reads those values, nothing happens.

And that is perfectly normal.

The dialog is only the user interface.

The actual behavior of the plugin is defined by the Python code that reads and uses those values.


Where Are the Values Stored?

Every field displayed in a dialog is a Python object created from the .ui file designed with Qt Designer.

Common examples include:

  • QLineEdit → text field
  • QComboBox → drop-down list
  • QCheckBox → check box
  • QSpinBox → numeric input

These widgets are accessible through the dialog object, usually named dlg.

For example:

self.dlg.lineEdit

self.dlg.comboBox

self.dlg.checkBox

The value entered by the user is stored inside the widget itself.

There is no hidden storage location.

If your plugin needs the value, it must explicitly retrieve it from the widget.

This simple idea is the foundation of all user interaction in a QGIS plugin.


Reading Values: Simple Examples

Once you know where the values are stored, the next step is to retrieve them from the dialog widgets.

Each widget type has its own method for accessing the user’s input.

Text Field (QLineEdit)

text = self.dlg.lineEdit.text()

text contains whatever the user typed into the field.

Drop-Down List (QComboBox)

choice = self.dlg.comboBox.currentText()

choice contains the currently selected item.

Check Box (QCheckBox)

enabled = self.dlg.checkBox.isChecked()

enabled contains either True or False.

Numeric Field (QSpinBox)

value = self.dlg.spinBox.value()

value contains a numeric value.

At this stage, there is no need to understand every technical detail.

The important thing is to remember that each widget provides a specific method for accessing its value.


The Critical Moment: When to Read Values

A common beginner mistake is trying to read values before the user has validated the dialog.

In a typical QGIS plugin, values should only be retrieved after the dialog has been accepted.

For example:

result = self.dlg.exec_()

if result:
# read the values here

The call to exec_() displays the dialog and waits for the user to interact with it.

Only when the user clicks OK (or otherwise accepts the dialog) does the code continue executing.

If you try to read the values before exec_() is executed, you will usually get empty fields or default values.

The user must first fill in the dialog and confirm it before the plugin can reliably access the entered data.

This is one of the most important concepts to understand when working with Qt dialogs.

The dialog collects information from the user.

Your code retrieves that information only after the dialog has been validated.


A Useful Mental Model

You can think of a dialog as a form waiting to be completed.

The process always follows the same sequence:

  1. The plugin displays the dialog.
  2. The user enters information.
  3. The user clicks OK.
  4. The plugin retrieves the values.
  5. The plugin performs an action.

If one of these steps is missing, the workflow is incomplete.

Understanding this sequence makes it much easier to understand how QGIS plugins interact with users.


A Complete Minimal Example

Let’s put everything together in a simple and deliberately minimal example.

result = self.dlg.exec_()

if result:
text = self.dlg.lineEdit.text()
choice = self.dlg.comboBox.currentText()
enabled = self.dlg.checkBox.isChecked()

print("Text:", text)
print("Choice:", choice)
print("Option enabled:", enabled)

At this stage, the plugin performs:

  • no analysis,
  • no complex processing,
  • no interaction with QGIS layers.

The goal is simply to prove that the plugin can successfully retrieve information entered by the user.

Once you can do that, you have established the connection between the user interface and your Python code.

That is a major milestone in plugin development.


Why Does Nothing Happen If You Don’t Read the Values?

This is one of the most common questions asked by beginners.

A QGIS plugin is not an automatic form.

There is no hidden magic behind the interface.

When a user types text, selects an option, or checks a box, QGIS simply stores that information in the corresponding widget.

If your code never reads the value, it is never used.

Nothing happens because you have not instructed the plugin to do anything with the information.

This behavior is intentional.

It gives developers complete control over how the plugin behaves and what actions are performed.

User input only becomes useful when your code explicitly retrieves and uses it.


What You Have Learned

By the end of this article, you now understand:

  • where user-entered values are stored,
  • how to retrieve them in Python,
  • when those values should be read,
  • why nothing happens until the plugin explicitly uses them.

These concepts form the foundation of every interactive QGIS plugin.

Whether your plugin performs a simple calculation or a complex spatial analysis, it will almost always begin by collecting information from the user.


What’s Next?

So far, our plugin can retrieve information entered by the user.

However, it still does not do anything useful with that information.

In the next article, we will learn how to transform user input into concrete actions.

We will see how to:

  • validate user input,
  • apply business rules,
  • make decisions based on user choices,
  • control the behavior of a plugin.

Retrieving values is the first step.

Using them intelligently is what makes a plugin truly useful.


Next article: Developing a QGIS Plugin: Applying Business Logic to User Input


Si cet article vous a intéressé et que vous pensez qu'il pourrait bénéficier à d'autres personnes, n'hésitez pas à le partager sur vos réseaux sociaux en utilisant les boutons ci-dessous. Votre partage est apprécié !

Leave a Reply

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

Are you human? Please solve:Captcha