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);
    

    0_1511452515437_4e6c33bf-9fa3-4a49-b882-08db4c589bbd-imagen.png

    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);
    

    0_1511452722879_ef903860-7cc1-4646-b900-069b0945f85a-imagen.png

    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


  • Qt Champions 2016

    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.



  • @VRonin @mrjj Thank you very much to both of you. It is working perfectly now!!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.