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. Why we delete menu in custmosied contextMenuEvent

Why we delete menu in custmosied contextMenuEvent

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 6 Posters 4.0k 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.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on last edited by
    #1

    void myQLineEdit::contextMenuEvent(QContextMenuEvent* event) {

    QMenu* menu = new mQMenu;
    QAction* copyAction = new mQAction("Copy",this);
    copyAction->setShortcut(tr("Ctrl+C"));
    menu->addAction(copyAction);
    connect(copyAction,SIGNAL(triggered()),this,SLOT(copy()));

    QAction* selectAllAction = new QAction("Select All",this);
    connect(selectAllAction,SIGNAL(triggered()),this,SLOT(selectAll()));
    menu->addAction(selectAllAction);
    selectAllAction->setShortcut(tr("Ctrl+A"));
    menu->exec(event->globalPos());
    delete menu;
    event->accept();
    }

    At the end we have delete menu and what is significance of event=>accept

    raven-worxR 1 Reply Last reply
    0
    • Q Qt Enthusiast

      void myQLineEdit::contextMenuEvent(QContextMenuEvent* event) {

      QMenu* menu = new mQMenu;
      QAction* copyAction = new mQAction("Copy",this);
      copyAction->setShortcut(tr("Ctrl+C"));
      menu->addAction(copyAction);
      connect(copyAction,SIGNAL(triggered()),this,SLOT(copy()));

      QAction* selectAllAction = new QAction("Select All",this);
      connect(selectAllAction,SIGNAL(triggered()),this,SLOT(selectAll()));
      menu->addAction(selectAllAction);
      selectAllAction->setShortcut(tr("Ctrl+A"));
      menu->exec(event->globalPos());
      delete menu;
      event->accept();
      }

      At the end we have delete menu and what is significance of event=>accept

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Qt-Enthusiast
      if you don't delete it, it would result in a dangling pointer (=memory leak), since you create the menu on the heap whenever a context-menu event is received.

      Alternatively you can create the menu on the stack (no pointer) or even as a member variable of the widget and reuse it every time.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      2
      • Q Offline
        Q Offline
        Qt Enthusiast
        wrote on last edited by
        #3

        but in QT as I know the children objects are deleted automatically because of parent child relation

        jsulmJ C 2 Replies Last reply
        0
        • Q Qt Enthusiast

          but in QT as I know the children objects are deleted automatically because of parent child relation

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by jsulm
          #4

          @Qt-Enthusiast Your menu does not have any parent:

          QMenu* menu = new mQMenu;
          

          You need to pass the pointer to the parent as parameter to constructor.
          And in this case it is probably better to delete manually as the menus you create will be deleted when the parent is deleted, so they would consume memory until parent is deleted.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • Q Qt Enthusiast

            but in QT as I know the children objects are deleted automatically because of parent child relation

            C Offline
            C Offline
            c64zottel
            wrote on last edited by
            #5

            @Qt-Enthusiast This is true for QObjects and those classes derived from it.
            QContextMenuEvent is not derived from QObjects. The Qt documentation can help you find out, which objects are derived from QObjects.

            1 Reply Last reply
            0
            • Q Offline
              Q Offline
              Qt Enthusiast
              wrote on last edited by Qt Enthusiast
              #6

              QMenu* menu = new IN_CURRENT_POOL mQMenu(this);
              //menu->addAction(QObject::tr("Copy"), this, SLOT(copy()),QKeySequence(tr("Ctrl+C")));
              menu->addAction(QObject::tr("Select All"), this, SLOT(copy()),QKeySequence(tr("Ctrl+A")));

              QAction* copyAction = new IN_CURRENT_POOL mQAction("Copy",this);
              copyAction->setShortcut(tr("Ctrl+C"));
              menu->addAction(copyAction);
              connect(copyAction,SIGNAL(triggered()),this,SLOT(copy()));

              /QAction selectAllAction = new IN_CURRENT_POOL QAction("Select All",this);
              connect(selectAllAction,SIGNAL(triggered()),this,SLOT(selectAll()));
              menu->addAction(selectAllAction);
              selectAllAction->setShortcut(tr("Ctrl+A"));*/

              menu->exec(event->globalPos());
              //delete selectAllAction;
              //delete copyAction;
              delete menu;

              event->accept();

              How to make the text color of Copy QAction to blue we select the Copy QAction

              1 Reply Last reply
              0
              • Q Offline
                Q Offline
                Qt Enthusiast
                wrote on last edited by
                #7

                QMenu* menu = new IN_CURRENT_POOL mQMenu(this);
                menu->addAction(QObject::tr("Copy"), this, SLOT(copy()),QKeySequence(tr("Ctrl+C")));
                menu->addAction(QObject::tr("Select All"), this, SLOT(copy()),QKeySequence(tr("Ctrl+A")));
                menu->exec(event->globalPos());
                delete menu;
                event->accept();
                }

                How to make the text menu to blue we select the Copy QAction

                jsulmJ 1 Reply Last reply
                0
                • Q Qt Enthusiast

                  QMenu* menu = new IN_CURRENT_POOL mQMenu(this);
                  menu->addAction(QObject::tr("Copy"), this, SLOT(copy()),QKeySequence(tr("Ctrl+C")));
                  menu->addAction(QObject::tr("Select All"), this, SLOT(copy()),QKeySequence(tr("Ctrl+A")));
                  menu->exec(event->globalPos());
                  delete menu;
                  event->accept();
                  }

                  How to make the text menu to blue we select the Copy QAction

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Qt-Enthusiast Why do you allocate on the heap? It is much easier to do it on the stack:

                  QMenu menu;
                  menu.addAction(QObject::tr("Copy"), this, SLOT(copy()),QKeySequence(tr("Ctrl+C")));
                  menu.addAction(QObject::tr("Select All"), this, SLOT(copy()),QKeySequence(tr("Ctrl+A")));
                  menu.exec(event->globalPos());
                  event->accept();
                  

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1
                  • Q Offline
                    Q Offline
                    Qt Enthusiast
                    wrote on last edited by
                    #9

                    What happens if I add the destructor in following code

                    class customLineEdit: public QLineEdit {
                    public:
                    customLineEdit(const QString& string);
                    ~customLineEdit() {}
                    virtual void contextMenuEvent(QContextMenuEvent *event);
                    };

                    customLineEdit::customLineEdit(const QString& text) {
                    setText(text);
                    // the user should not be able to edit the property colors
                    this->setReadOnly(true);
                    // RBG 236,236,236 stands for gray color
                    QColor rgb(236,236,236);
                    QString style = QString("background-color: rgb(%1,%2,%3) ;").arg(rgb.red()).arg(rgb.green()).arg(rgb.blue());
                    // style sheets for customising colors borders etc
                    this->setStyleSheet(style);

                    }

                    void customLineEdit::contextMenuEvent(QContextMenuEvent* event)
                    {
                    QMenu menu;
                    menu.addAction(QObject::tr("Copy"), this, SLOT(copy()),QKeySequence(tr("Ctrl+C")));
                    menu.addAction(QObject::tr("Select All"), this, SLOT(copy()),QKeySequence(tr("Ctrl+A")));
                    menu.exec(event->globalPos());
                    event->accept();
                    }

                    E jsulmJ 2 Replies Last reply
                    0
                    • Q Qt Enthusiast

                      What happens if I add the destructor in following code

                      class customLineEdit: public QLineEdit {
                      public:
                      customLineEdit(const QString& string);
                      ~customLineEdit() {}
                      virtual void contextMenuEvent(QContextMenuEvent *event);
                      };

                      customLineEdit::customLineEdit(const QString& text) {
                      setText(text);
                      // the user should not be able to edit the property colors
                      this->setReadOnly(true);
                      // RBG 236,236,236 stands for gray color
                      QColor rgb(236,236,236);
                      QString style = QString("background-color: rgb(%1,%2,%3) ;").arg(rgb.red()).arg(rgb.green()).arg(rgb.blue());
                      // style sheets for customising colors borders etc
                      this->setStyleSheet(style);

                      }

                      void customLineEdit::contextMenuEvent(QContextMenuEvent* event)
                      {
                      QMenu menu;
                      menu.addAction(QObject::tr("Copy"), this, SLOT(copy()),QKeySequence(tr("Ctrl+C")));
                      menu.addAction(QObject::tr("Select All"), this, SLOT(copy()),QKeySequence(tr("Ctrl+A")));
                      menu.exec(event->globalPos());
                      event->accept();
                      }

                      E Offline
                      E Offline
                      Eeli K
                      wrote on last edited by
                      #10

                      @Qt-Enthusiast Do you mean this line: "~customLineEdit() {}" which you added already? Apparently it does nothing, and it's not needed at all, because "The purpose of the destructor is to free the resources that the object may have acquired during its lifetime." (http://en.cppreference.com/w/cpp/language/destructor) Your class doesn't acquire resources. The base class has its own destructor and it's executed automatically. See also http://en.cppreference.com/w/cpp/language/rule_of_three: "If a class requires a user-defined destructor, a user-defined copy constructor, or a user-defined copy assignment operator, it almost certainly requires all three." So, don't add it if you don't need it.

                      1 Reply Last reply
                      1
                      • Q Qt Enthusiast

                        What happens if I add the destructor in following code

                        class customLineEdit: public QLineEdit {
                        public:
                        customLineEdit(const QString& string);
                        ~customLineEdit() {}
                        virtual void contextMenuEvent(QContextMenuEvent *event);
                        };

                        customLineEdit::customLineEdit(const QString& text) {
                        setText(text);
                        // the user should not be able to edit the property colors
                        this->setReadOnly(true);
                        // RBG 236,236,236 stands for gray color
                        QColor rgb(236,236,236);
                        QString style = QString("background-color: rgb(%1,%2,%3) ;").arg(rgb.red()).arg(rgb.green()).arg(rgb.blue());
                        // style sheets for customising colors borders etc
                        this->setStyleSheet(style);

                        }

                        void customLineEdit::contextMenuEvent(QContextMenuEvent* event)
                        {
                        QMenu menu;
                        menu.addAction(QObject::tr("Copy"), this, SLOT(copy()),QKeySequence(tr("Ctrl+C")));
                        menu.addAction(QObject::tr("Select All"), this, SLOT(copy()),QKeySequence(tr("Ctrl+A")));
                        menu.exec(event->globalPos());
                        event->accept();
                        }

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        @Qt-Enthusiast What do you expect to happen if you add a destructor which does not do anything? So, what is your question about?

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • Q Offline
                          Q Offline
                          Qt Enthusiast
                          wrote on last edited by
                          #12

                          Since we have a automatic deletion of Object QT . If I put the destructor then hope for ~customLineEdit() {} I assume it will not any impact ?

                          J.HilkJ jsulmJ 2 Replies Last reply
                          0
                          • Q Qt Enthusiast

                            Since we have a automatic deletion of Object QT . If I put the destructor then hope for ~customLineEdit() {} I assume it will not any impact ?

                            J.HilkJ Offline
                            J.HilkJ Offline
                            J.Hilk
                            Moderators
                            wrote on last edited by
                            #13

                            @Qt-Enthusiast

                            Adding a destructor without anything in it as no effect and will most likly be
                            rationalized by the compiler.

                            Also, adding a destructor to you class does not overwrite the normal destruction of the class, see it more like a notfier slot.
                            A function beeing called when the class-object is about to be destroyed, therefore allowing you to free any manually allocated memory.


                            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                            Q: What's that?
                            A: It's blue light.
                            Q: What does it do?
                            A: It turns blue.

                            1 Reply Last reply
                            0
                            • Q Qt Enthusiast

                              Since we have a automatic deletion of Object QT . If I put the destructor then hope for ~customLineEdit() {} I assume it will not any impact ?

                              jsulmJ Offline
                              jsulmJ Offline
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @Qt-Enthusiast You do not need to add own destructor to use the automatic deletion of QObject derived classes! The only thing you need to do is: pass parent pointer to the child constructor. Then, if parent is deleted, the child will be deleted as well. It is explained here: http://doc.qt.io/qt-5/objecttrees.html
                              Adding an empty destructor does not have any effects (how should it?).

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              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