Solved Creating a QMenu associated to a QPushButton
-
Hello!!
I am trying to associate a QMenu to a QPushButton, but I have a little problem. In this first case, it creates the menu correctly:
menuSettings = new QMenu(this); ui->settingsButton->setMenu(menuSettings); QFile settingsStyleFile(QCoreApplication::applicationDirPath() + "/menuSettingsStyle.css"); settingsStyleFile.open(QIODevice::ReadOnly); QString style = QLatin1String(settingsStyleFile.readAll()); menuSettings->setStyleSheet(style); menuSettings->addAction("Config CAMS"); menuSettings->addAction("Alarms"); menuSettings->addAction("Storage"); menuSettings->addAction("Plano"); menuSettings->addAction("Advance"); menuSettings->addAction("Report"); ui->settingsButton->setMenu(menuSettings);
But, when I try to give it functionality, the menu doesn't appear:
menuSettings = new QMenu(this); ui->settingsButton->setMenu(menuSettings); QFile settingsStyleFile(QCoreApplication::applicationDirPath() + "/menuSettingsStyle.css"); settingsStyleFile.open(QIODevice::ReadOnly); QString style = QLatin1String(settingsStyleFile.readAll()); menuSettings->setStyleSheet(style); QAction actionConfigCams("Config CAMS", ui->settingsButton); connect (&actionConfigCams, SIGNAL(triggered()), this, SLOT(showConfigCamsTab())); QAction actionAlarms("Alarms", ui->settingsButton); connect (&actionAlarms, SIGNAL(triggered()), this, SLOT(showAlarmsTab())); QAction actionStorage("Storage", ui->settingsButton); connect (&actionStorage, SIGNAL(triggered()), this, SLOT(showStorageTab())); QAction actionPlano("Plano", ui->settingsButton); connect (&actionPlano, SIGNAL(triggered()), this, SLOT(showPlanoTab())); QAction actionAdvance("Advance", ui->settingsButton); connect (&actionAdvance, SIGNAL(triggered()), this, SLOT(showAdvanceTab())); QAction actionReport("Report", ui->settingsButton); connect (&actionReport, SIGNAL(triggered()), this, SLOT(showReportTab())); menuSettings->addAction(&actionConfigCams); menuSettings->addAction(&actionAlarms); menuSettings->addAction(&actionStorage); menuSettings->addAction(&actionPlano); menuSettings->addAction(&actionAdvance); menuSettings->addAction(&actionReport); ui->settingsButton->setMenu(menuSettings);
Anybody knows how could be the problem?
Thank you very much!!
-
Your QActions are on the stack and will die at the end of the constructor. allocate them with
new
-
hi
You create Action as local variables.
So unless they survive after the function ends
its pretty sure they not going to fire any signals.Whenever you see an & in your connect
You must ask yourself
Will this object live long enough to later send any signals.Not sure its that but since addAction expects a pointer its very likely it wont copy
the Action and hence u are giving it an object that is soon to die. -