QT: signals from QMenu and QAction -
my problem make menu load files. here's code:
qstringlist filenamelist; filenamelist << "file1" << "file2" << "file3"; qmenubar *menubar = new qmenubar(); qmenu *menu = menubar->addmenu("file"); qmenu *load = menu->addmenu("load"); foreach (qstring filename, filenamelist) { qaction *loadfile = new qaction(filename, this); load->addaction(loadfile); connect(load,signal(triggered(qaction*)),this, slot(load(qaction*))); }
and slot:
void mainwindow::load(qaction* action) { qdebug() << action->text(); }
after click action button, qdebug shows:
"file1" "file1" "file1"
but need run action once! qaction not have signal can name. how solve this? thank you!
the problem create same connection tree times in loop. need, doing once:
[..] foreach (qstring filename, filenamelist) { qaction *loadfile = new qaction(filename, this); load->addaction(loadfile); } connect(load, signal(triggered(qaction *)), this, slot(load(qaction *)));
update
the alternative solution be:
foreach (qstring filename, filenamelist) { qaction *loadfile = new qaction(filename, this); load->addaction(loadfile); connect(loadfile, signal(triggered()), this, slot(load())); }
with corresponding slot:
void mainwindow::load() { qaction *action = qobject_cast<qaction *>(sender()); if (action) qdebug() << action->text(); }
Comments
Post a Comment