Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-63556

QMenu clear does not delete nested menus

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P3: Somewhat important
    • None
    • 5.10.0 Alpha
    • None

    Description

      The QMenu::clear API

      http://doc.qt.io/qt-5/qmenu.html#clear

      deletes top-level actions, but not submenus or the actions within them.

          #include <QApplication>
          #include <QToolButton>
          #include <QMainWindow>
          #include <QMenu>
          #include <QAction>
          #include <QDebug>
          #include <QHBoxLayout>
          #include <QPushButton>
          #include <QPointer>
      
          class MyAction : public QAction 
          {
              Q_OBJECT
          public:
              MyAction(const QString &nm, QObject* parent = {})
                  : QAction(parent)
              {
                  m_name = nm;
                  setText(nm);
              }
      
          private:
              QString m_name;
          };
      
          class MyWidget : public QWidget
          {
              Q_OBJECT
          public:
              MyWidget(QWidget* parent = {})
                : QWidget(parent)
              {
                  auto toolbutton = new QToolButton(this);
                  toolbutton->setText("Tool");
                  toolbutton->setPopupMode(QToolButton::InstantPopup);
      
                  auto layout = new QHBoxLayout(this);
                  layout->addWidget(toolbutton);
      
                  m_newComponentMenu = new QMenu("MyMenu", toolbutton);
                  toolbutton->setMenu(m_newComponentMenu);
      
                  m_action1 = new MyAction("action1", m_newComponentMenu);
                  m_newComponentMenu->addAction(m_action1.data());
      
                  m_nestedMenu = new QMenu("Nested", m_newComponentMenu);
                  m_newComponentMenu->addMenu(m_nestedMenu);
              
                  m_action2 = new MyAction("action2", m_nestedMenu.data());
                  m_nestedMenu->addAction(m_action2.data());
      
                  auto clearButton = new QPushButton("Clear");
                  layout->addWidget(clearButton);
                  connect(clearButton, &QPushButton::clicked, this, &MyWidget::clear);
              }
      
          private Q_SLOTS:
              void clear()
              {
                  qDebug() << "\n";
                  qDebug() << "Actions before" << m_newComponentMenu->actions();
                  qDebug() << "Action1 before:" << m_action1.data();
                  qDebug() << "Nested menu before:" << m_nestedMenu.data();
                  qDebug() << "Action2 before:" << m_action2.data();
      
                  m_newComponentMenu->clear();
      
                  qDebug() << "\n";
                  qDebug() << "Actions after:" << m_newComponentMenu->actions();
                  qDebug() << "Action1 after:" << m_action1.data();
                  qDebug() << "Nested menu after:" << m_nestedMenu.data();
                  qDebug() << "Action2 after:" << m_action2.data();
              }
      
          private:
              QMenu* m_newComponentMenu;
              QPointer<MyAction> m_action1;
              QPointer<MyAction> m_action2;
              QPointer<QMenu> m_nestedMenu;
          };
      
          int main(int argc, char** argv)
          {
              QApplication app(argc, argv);
      
              MyWidget mw;
              mw.resize(400, 400);
              mw.show();
      
              return app.exec();
          }
      
          #include "nestedmenumemory.moc"
      

      Expected output:

          Actions after: ()
          Action1 after: QAction(0x0)
          Nested menu after: QMenu(0x0)
          Action2 after: QAction(0x0)

      Actual output:    

          Actions after: ()
          Action1 after: QAction(0x00)
          Nested menu after: QMenu(0x25ebadadf90)
          Action2 after: QAction(0x25ebad93140 text="action2" toolTip="action2" menuRole=TextHeuristicRole visible=true)

       

       

       

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            richard Richard Moe Gustavsen
            stkellyms Stephen Kelly (Work)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes