5

I'm trying QgsCheckableComboBox() in my simple python script in QGIS. How to pause code execution while a comboBox-window is open (like a dialog box)? In my example, I want to ensure that the print() function is executed only after the comboBox-window is closed. Then the checkedItems list will not be empty and will contain the selected lines ("Venus", "Earth", "Mars"). How can I achieve this?

from qgis.PyQt import QtGui
from qgis.core import *

planet_list = ["Venus", "Earth", "Mars", "Jupiter", "Pluto"]
items = QgsCheckableComboBox()
items.addItems(planet_list)
items.showNormal()

print('\n\n-----------CheckedItems: ', items.checkedItems())

Example

2
  • Have you tried any other approaches than the code posted? And can you clarify, the code currently executes, but the print() is not waiting for the combo box interaction?
    – jcarlson
    Commented Oct 18, 2020 at 20:49
  • No, I have not tried other approaches. Yes that's right, the print() is not waiting for the combo box interaction, but your code solved my problem! Commented Oct 19, 2020 at 1:29

1 Answer 1

6

You can add the combobox to a dialog window. After closing the dialog, print prints the selected items.

from qgis.PyQt import QtGui
from qgis.core import *

planet_list = ["Venus", "Earth", "Mars", "Jupiter", "Pluto"]
items = QgsCheckableComboBox()
items.addItems(planet_list)

dlg = QDialog()
layout = QVBoxLayout()
layout.addWidget(items)
dlg.setLayout(layout)
dlg.exec_()

print('\n\n-----------CheckedItems: ', items.checkedItems())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.