Im trying to make my menu auto generated using this for loop:
q3 = connect_menu.addMenu('Q3')
q3menus = {}
x = 0
for element in q3s:
key = 'Q'+str(x)
q3menus[key] = QAction(element[7], self)
q3.addAction(q3menus[key])
q3menus[key].triggered.connect(lambda: self.main_form.consavesv(q3menus[key].text()))
x += 1
q3s
equals to a Fetchall function that gets the data from last SQL Query
q3s = [(1, 'Q3', '127.0.0.1', 28960, "Server Test Name", '5154', '127.0.0.1:28960', "127.0.0.1:28960 | Server Test Name"), (2, 'Q3', '192.168.1.66', 28960, 'Server Test 2', '5154', '192.168.1.66:28960', '192.168.1.66:28960 | Server Test 2')]
I tried to use a dictianory, This dictianory adds menu labels as well as it should, but when I click on them I they all have the same triggered.connect()
as the last one does..
What Im doing wrong ?
I dont want to create Signals myself in here, Cause the Database Results are not static..
Sorry for bad English.
he problem is the way you pass the parameters to a lambda function, the correct syntax is:
f = lambda p_1, p_2, ..., p_n: some operation with p_1, p_2, ..., p_n
In your case you must also take into account that the triggered signal of QAction
returns a value called checked, so you must precede that parameter as shown below:
q3menus[key].triggered.connect(lambda checked, key=key: self.main_form.consavesv(q3menus[key].text()))
In addition to this approach you can also use the sender()
method that returns the object that emits the signal and get the text directly as shown in the following example:
class Example(QMainWindow):
def __init__(self, parent = None):
QMainWindow.__init__(self, parent)
q3s = [(1, 'Q3', '127.0.0.1', 28960, "Server Test Name", '5154', '127.0.0.1:28960', "127.0.0.1:28960 | Server Test Name"),
(2, 'Q3', '192.168.1.66', 28960, 'Server Test 2', '5154', '192.168.1.66:28960', '192.168.1.66:28960 | Server Test 2')]
menubar = self.menuBar()
q3 = menubar.addMenu("Q")
q3menus = {}
for x, element in enumerate(q3s):
key = 'Q'+str(x)
q3menus[key] = QAction(element[7], self)
q3.addAction(q3menus[key])
q3menus[key].triggered.connect(lambda checked, key=key : self.someFunction(q3menus[key].text()))
q3menus[key].triggered.connect(self.anotherFuntions)
def someFunction(self, text):
print("someFunction {}".format(text))
def anotherFuntions(self):
print("anotherFuntions {}".format(self.sender().text()))
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Example()
w.show()
sys.exit(app.exec_())