Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Creating a QMenu associated to a QPushButton

Creating a QMenu associated to a QPushButton

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ivanicyI Offline
    ivanicyI Offline
    ivanicy
    wrote on last edited by
    #1

    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!!

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      Your QActions are on the stack and will die at the end of the constructor. allocate them with new

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      ivanicyI 1 Reply Last reply
      5
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        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.

        1 Reply Last reply
        4
        • VRoninV VRonin

          Your QActions are on the stack and will die at the end of the constructor. allocate them with new

          ivanicyI Offline
          ivanicyI Offline
          ivanicy
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved