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. Exception when passing pointer to QMenu
Forum Updated to NodeBB v4.3 + New Features

Exception when passing pointer to QMenu

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 460 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.
  • P Offline
    P Offline
    Please_Help_me_D
    wrote on 6 Sept 2020, 16:06 last edited by Please_Help_me_D 9 Jun 2020, 16:09
    #1

    Hi,

    I have a base class for QTreeView (H5TreeView) where I add context menu when pressing on header. This base class has one QAction. I want that all subclasses from H5TreeView had opportunity to add their QActions.
    Here is the code for H5TreeView:
    h5treeview.h:

    #ifndef H5TREEVIEW_H
    #define H5TREEVIEW_H
    
    #include <QTreeView>
    
    class H5TreeView : public QTreeView
    {
        Q_OBJECT
    
    public:
        H5TreeView(QWidget *parent = nullptr);
    
    protected:
        ///
        /// \brief for subclasses to add actions
        /// \param menu add actions here
        ///
        virtual void fillHdrMenu(QMenu* menu);
    
    private:
        void hdrMenuRequested(QPoint pos);
    };
    
    #endif // H5TREEVIEW_H
    

    h5treeview.cpp:

    #include "h5treeview.h"
    
    #include "h5proxymodel.h"
    
    #include <QHeaderView>
    #include <QMenu>
    #include <QAction>
    
    H5TreeView::H5TreeView(QWidget *parent):
        QTreeView(parent)
    {
        setSortingEnabled(true);
        setContextMenuPolicy(Qt::CustomContextMenu);
        header()->setContextMenuPolicy(Qt::CustomContextMenu);
    
        connect(header(), &QTreeView::customContextMenuRequested,
                this, &H5TreeView::hdrMenuRequested);
    }
    
    void H5TreeView::fillHdrMenu(QMenu* menu){
    
    }
    
    void H5TreeView::hdrMenuRequested(QPoint pos){
        QMenu *menu = new QMenu(this);
    
        QAction *checkedOnlyAct = menu->addAction("Display checked only");
        checkedOnlyAct->setCheckable(true);
    
        H5ProxyModel *proxyModel = qobject_cast<H5ProxyModel*>(model());
        if (proxyModel)
            checkedOnlyAct->setChecked(proxyModel->isShowCheckedOnly());
        else
            checkedOnlyAct->setDisabled(true);
    
        connect(checkedOnlyAct, &QAction::toggled, proxyModel, &H5ProxyModel::setShowCheckedItemsOnly);
        menu->addSeparator();
    
        /* HERE I INVOKE THAT METHOD FOR SUBCLASSES TO ADD ACTIONS TO QMENU */
        /* without it the app works fine */
        fillHdrMenu(menu);
    
        menu->popup(header()->mapToGlobal(pos));
    }
    

    As you can see it has void H5TreeView::fillHdrMenu(QMenu* menu) that all inheritors have. Even my inheritor leave void H5SeisTreeView::fillHdrMenu(QMenu* menu) empty I get the exception:
    baed61a6-e85d-4660-9e82-43dea356699c-image.png

    Please explain what is going wrong...

    1 Reply Last reply
    0
    • P Please_Help_me_D
      6 Sept 2020, 19:08

      @Christian-Ehrlicher I 've found the problem!

      All beacause of that I used to set parent for QMenu:

      QMenu *menu = new QMenu(this);
      

      As I have few TreeViews each in custom dockWidget that lead to CDockWidgets are destroyed in some order and thus when I close the app it somehow used to lead to an exception (parent this is destroyed thus menu is also destroyed but TreeView tries to send it... I think something like that).

      Now I create QMenu without parent but will this lead to memory leak?

      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 6 Sept 2020, 19:20 last edited by
      #9

      @Please_Help_me_D
      Hi
      Yes if QMenu has no parent it never is deleted and hence a leak.
      So if you call hdrMenuRequested many times, you will leak a QMenu which might not be good.

      You could use
      https://doc.qt.io/qt-5/qmenu.html#exec
      which is blocking and then delete after.

      menu->exec(header()->mapToGlobal(pos));
      menu->deletelater();

      P 1 Reply Last reply 6 Sept 2020, 19:30
      1
      • C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 6 Sept 2020, 16:28 last edited by
        #2

        @Please_Help_me_D said in Exception when passing pointer to QMenu:

        Please explain what is going wrong...

        take a look at the backtrace to see where in your code you call it.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        P 1 Reply Last reply 6 Sept 2020, 16:32
        0
        • C Christian Ehrlicher
          6 Sept 2020, 16:28

          @Please_Help_me_D said in Exception when passing pointer to QMenu:

          Please explain what is going wrong...

          take a look at the backtrace to see where in your code you call it.

          P Offline
          P Offline
          Please_Help_me_D
          wrote on 6 Sept 2020, 16:32 last edited by
          #3

          @Christian-Ehrlicher said in Exception when passing pointer to QMenu:

          take a look at the backtrace to see where in your code you call it.

          I'm sorry, I didn't understand what does it mean. What is a "backtrace"?
          I call fillHdrMenu(QMenu* menu) only inside hdrMenuRequested(QPoint pos) of base H5TreeView class

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 6 Sept 2020, 16:38 last edited by
            #4

            @Please_Help_me_D said in Exception when passing pointer to QMenu:

            What is a "backtrace"?

            https://doc.qt.io/qtcreator/creator-debug-mode.html#viewing-call-stack-trace

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            P 1 Reply Last reply 6 Sept 2020, 16:46
            0
            • C Christian Ehrlicher
              6 Sept 2020, 16:38

              @Please_Help_me_D said in Exception when passing pointer to QMenu:

              What is a "backtrace"?

              https://doc.qt.io/qtcreator/creator-debug-mode.html#viewing-call-stack-trace

              P Offline
              P Offline
              Please_Help_me_D
              wrote on 6 Sept 2020, 16:46 last edited by
              #5

              @Christian-Ehrlicher thank you for information
              b1142057-87d5-4e3c-9104-cc9d13741902-image.png

              qwindowsd is it? This file is not from my project. Actually all the files from that dim grey list are not from my project :)

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 6 Sept 2020, 16:48 last edited by
                #6

                I'm pretty sure it has nothing to do with the code you show us above. Please make sure that your build dir is clean and do a full rebuild. If it still happens please provide a minimal, compilable example so we can take a look.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                P 2 Replies Last reply 6 Sept 2020, 18:08
                0
                • C Christian Ehrlicher
                  6 Sept 2020, 16:48

                  I'm pretty sure it has nothing to do with the code you show us above. Please make sure that your build dir is clean and do a full rebuild. If it still happens please provide a minimal, compilable example so we can take a look.

                  P Offline
                  P Offline
                  Please_Help_me_D
                  wrote on 6 Sept 2020, 18:08 last edited by
                  #7

                  @Christian-Ehrlicher there is no such exception in test project.
                  I understand that you can't help me now but one last thing: I get this exception only when I close the application. Do you have some ideas where to look?

                  If possible can you translate what assembler code means at the line 117024 from picture above? Can I get information about what pointer gives me such exception?

                  1 Reply Last reply
                  0
                  • C Christian Ehrlicher
                    6 Sept 2020, 16:48

                    I'm pretty sure it has nothing to do with the code you show us above. Please make sure that your build dir is clean and do a full rebuild. If it still happens please provide a minimal, compilable example so we can take a look.

                    P Offline
                    P Offline
                    Please_Help_me_D
                    wrote on 6 Sept 2020, 19:08 last edited by
                    #8

                    @Christian-Ehrlicher I 've found the problem!

                    All beacause of that I used to set parent for QMenu:

                    QMenu *menu = new QMenu(this);
                    

                    As I have few TreeViews each in custom dockWidget that lead to CDockWidgets are destroyed in some order and thus when I close the app it somehow used to lead to an exception (parent this is destroyed thus menu is also destroyed but TreeView tries to send it... I think something like that).

                    Now I create QMenu without parent but will this lead to memory leak?

                    M 1 Reply Last reply 6 Sept 2020, 19:20
                    0
                    • P Please_Help_me_D
                      6 Sept 2020, 19:08

                      @Christian-Ehrlicher I 've found the problem!

                      All beacause of that I used to set parent for QMenu:

                      QMenu *menu = new QMenu(this);
                      

                      As I have few TreeViews each in custom dockWidget that lead to CDockWidgets are destroyed in some order and thus when I close the app it somehow used to lead to an exception (parent this is destroyed thus menu is also destroyed but TreeView tries to send it... I think something like that).

                      Now I create QMenu without parent but will this lead to memory leak?

                      M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 6 Sept 2020, 19:20 last edited by
                      #9

                      @Please_Help_me_D
                      Hi
                      Yes if QMenu has no parent it never is deleted and hence a leak.
                      So if you call hdrMenuRequested many times, you will leak a QMenu which might not be good.

                      You could use
                      https://doc.qt.io/qt-5/qmenu.html#exec
                      which is blocking and then delete after.

                      menu->exec(header()->mapToGlobal(pos));
                      menu->deletelater();

                      P 1 Reply Last reply 6 Sept 2020, 19:30
                      1
                      • M mrjj
                        6 Sept 2020, 19:20

                        @Please_Help_me_D
                        Hi
                        Yes if QMenu has no parent it never is deleted and hence a leak.
                        So if you call hdrMenuRequested many times, you will leak a QMenu which might not be good.

                        You could use
                        https://doc.qt.io/qt-5/qmenu.html#exec
                        which is blocking and then delete after.

                        menu->exec(header()->mapToGlobal(pos));
                        menu->deletelater();

                        P Offline
                        P Offline
                        Please_Help_me_D
                        wrote on 6 Sept 2020, 19:30 last edited by
                        #10

                        @mrjj thank you! now it works without exceptions and QMenu has parent

                        1 Reply Last reply
                        1

                        9/10

                        6 Sept 2020, 19:20

                        • Login

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