Question

I am implementing run time menu on a button(QPushButton) in my app user has to make choice between ftp and http when he click on upload button.

QMenu menu(this); 
menu.addAction("ftp");
menu.addAction("http");
ui.mUploadBtn->setMenu(&menu);
ui.mUploadBtn->showMenu();

My ques is that how could i know which selection user selected or is their any function to get that.

thankyou

Was it helpful?

Solution

One way to catch the result of a menu is to do this :

 QAction* action = menu.exec(QCursor::pos());

Then you can use

action->text()==QString("ftp")

To know if the user clicked on "FTP".

So as said by Frank Osterfeld in the comments, you can also create a slot in you class, and connect menu trigger action signal to it :

connect(&menu, SIGNAL(triggered(QAction*)), this, SLOT( sl_OnMenuTriggered(QAction* )));

void sl_OnMenuTriggered(QAction* _action)
{
if(_action->text()=="ftp") ...
}

This is the "non blocking" way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top