开发者

In PyQt, how can signals and slots be used to connect a dropdownlist to a function?

开发者 https://www.devze.com 2022-12-18 21:25 出处:网络
In PyQt, there\'s a concept of signals and slots to connect objects to one another\'s functions, but I can\'t seem to find them referenced to functions not associated with other objects.For example, I

In PyQt, there's a concept of signals and slots to connect objects to one another's functions, but I can't seem to find them referenced to functions not associated with other objects. For example, I want a dropdown list to have algorithm A or algorithm B run.

How does PyQt acco开发者_运维百科mplish this functionality?


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

Connect the dropdown list's appropriate signal to your function.

For example with the QComboBox currentIndexChanged() signal. Connect that to a "wrapper" function that decides (based on the index) which function to call.

Edit: The wrapper can be very simple, like so:

functions = {0: reference_to_function_1, 1: reference_to_function_2}

def wrapper(index):
    functions[index]()

Edit2: If you want some alternate methods for connecting slots:

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

Note when they are talking about Py or Qt signals, and when they are talking about Python functions or methods. E.g., these are the syntax for connecting a Qt signal to a Python function and a Python method:

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

精彩评论

暂无评论...
验证码 换一张
取 消