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. How to access dynamically created QMenu for tests purposes
Qt 6.11 is out! See what's new in the release blog

How to access dynamically created QMenu for tests purposes

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 2.2k 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.
  • K Offline
    K Offline
    KelvinSP
    wrote on last edited by
    #1

    Hi, I have a QwtPlot object and I'm providing a way to save the graph as an image.

    In the constructor, I'm defining this:

    qwtPlot->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(qwtPlot, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(popUpMenu(QPoint)));
    

    Then, I have created the popUpMenu function:

    void MyClass::popUpMenu(QPoint pos)
    {
        QMenu menu;
        QAction* actionSave = menu.addAction(QIcon(":/prefix/SaveImageIcon.png"), tr("Save Image"));
        connect(actionSave, SIGNAL(triggered()), this, SLOT(saveAsImage()));
        menu.popup(pos);
        menu.exec(QCursor::pos());
    }
    

    And the saveAsImage function:

    void MyClass::saveAsImage()
    {
        if( qwtPlot->itemList().isEmpty() )
        {
            return;
        }
    
        saveWidgetAsImage(qwtPlot);
    }
    

    Now, I'm developing another Qt application to test this one. It accesses the MainWindow and all its widgets to test the graphical user interface.

    As I have access to the widgets I can emit the customContextMenuRequested signal as follows:

    emit qwtPlot->customContextMenuRequested(QPoint(10,10));
    

    It works and shows the QMenu, but now I want to access the QMenu to call the QAction responsible for saving the widget as an image. Is it possible? How can I do that?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      Im not sure I understand.

      If you can call the
      qwtPlot->customContextMenuRequested(QPoint(10,10));
      wont it call void MyClass::popUpMenu(QPoint pos)
      that will show menu and the action already there , ready to call void MyClass::saveAsImage() when clicked?

      Also do you mean access to the local variable ?
      void MyClass::popUpMenu(QPoint pos)
      {
      QMenu menu; <<< this QMenu ?

      K 1 Reply Last reply
      0
      • mrjjM mrjj

        Hi
        Im not sure I understand.

        If you can call the
        qwtPlot->customContextMenuRequested(QPoint(10,10));
        wont it call void MyClass::popUpMenu(QPoint pos)
        that will show menu and the action already there , ready to call void MyClass::saveAsImage() when clicked?

        Also do you mean access to the local variable ?
        void MyClass::popUpMenu(QPoint pos)
        {
        QMenu menu; <<< this QMenu ?

        K Offline
        K Offline
        KelvinSP
        wrote on last edited by
        #3

        @mrjj thanks.

        Yes, the popUpMenu is being called, and the QMenu is also being shown. Now I need to find a way to trigger the QAction (actionSave) programmatically.

        I think I found a workaround to do it. I will post it here.

        1 Reply Last reply
        1
        • K Offline
          K Offline
          KelvinSP
          wrote on last edited by KelvinSP
          #4

          The Qt documentation says:

          ...a popup menu is always a top-level widget, if a parent is passed the popup menu will be deleted when that parent is destroyed...
          

          So, I have added a parent for the QMenu and assigned an object name:

          QMenu *menu = new QMenu(this);
          menu->setObjectName("PopUpMenu");
          QAction* actionSave = menu->addAction(QIcon(":/prefix/SaveAsImageIcon.png"), tr("Save as image"));
          connect(actionSave, SIGNAL(triggered()), this, SLOT(saveAsImage()));
          menu->popup(pos);
          menu->exec(QCursor::pos());
          

          Now, I can search for the QMenu widget and find the correct menu using the objectName. Finding the QMenu I can trigger the action:

          QWidgetList allToplevelWidgets = QApplication::topLevelWidgets();
          foreach(QWidget *w, allToplevelWidgets)
          {
              if( w->inherits("QMenu") )
              {
                  QMenu *menu = qobject_cast<QMenu *>(w);
                  if( menu->objectName() == "PopUpMenu" )
                  {
                      menu->actions().at(0)->trigger();
                      break;
                  }
              }
          }
          
          1 Reply Last reply
          1
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Hi
            So the goal is to make it save image without user clicking the action?
            Ahh, i missed the testing part. So its do module test.

            1 Reply Last reply
            1

            • Login

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