In PyQt, how can I use signals and slots to connect a drop-down list to a function?

In PyQt, there is the concept of signals and slots for connecting objects to each other, but I can not find them, referring to functions that are not related to other objects. For example, I want Algorithm A or Algorithm B to be launched in the drop-down list.

How does PyQt perform this function?

+4
source share
1 answer

Do you need the effect of changing a drop-down list to call a function?

Connect the drop-down list of the corresponding signal to your function.

For example, using QComboBox currentIndexChanged (). Connect this to the wrapper function, which decides (based on the index) which function to call.

Edit: a wrapper can be very simple, for example:

functions = {0: reference_to_function_1, 1: reference_to_function_2} def wrapper(index): functions[index]() 

Edit2: If you want to use alternative methods for connecting slots:

http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/pyqt4ref.html#connecting-signals-and-slots

Notice when they talk about Py or Qt signals, and when they talk about Python functions or methods. For example, this is the syntax for connecting a Qt signal to a Python function and Python method:

 QtCore.QObject.connect(a, QtCore.SIGNAL("QtSig()"), pyFunction) #function-style QtCore.QObject.connect(a, QtCore.SIGNAL("QtSig()"), pyClass.pyMethod) #method-style 
+3
source

Source: https://habr.com/ru/post/1299903/


All Articles