qt5 - How to select file from MainMenu in Qt? -
i load text files folder mainmenu in qt application.
void mainwindow::loadfilestomainmenu() { qstring pathtodir("/myfiles"); qdiriterator it(pathtodir, qstringlist() << "*.txt", qdir::files, qdiriterator::subdirectories); while (it.hasnext()) { qstring curpathname = it.next(); qstringlist filesegments = curpathname.split('/'); qstring curfilename = filesegments.at(filesegments.size() - 1); qaction* action = new qaction(tr(curfilename.tostdstring().c_str()), this); action->setstatustip(tr(curpathname.tostdstring().c_str())); ui->menufilelist->addaction(action); // if new style selected? connect(action, signal(triggered()), this, slot(onloadfile())); } }
there create qactions files in folder 'myfiles' , connect these each of these ations slot onloadfile():
void mainwindow::onloadfile() { qaction *action = qobject_cast<qaction *>(sender()); if (action) { qdebug() << " onloadfile " << action->data().tostring(); } }
so each time select 1 of files in mainmenu, slot triggered, debug message says:
onloadfile ""
when instance select /myfiles/file1.txt
onloadfile "/myfiles/file1.txt"
wham missing? thanx in advance
the answer @m.s. solves question well...
you should use qaction::setdata() before trying read data – m.s
Comments
Post a Comment