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. QTextEdit (clear on right mouse button)

QTextEdit (clear on right mouse button)

Scheduled Pinned Locked Moved Unsolved General and Desktop
19 Posts 5 Posters 8.0k Views 4 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.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #8

    @Qt-Enthusiast said:

    createStandardContextMenu

    Creates a new Menu
    doc says
    http://doc.qt.io/qt-5/qtextedit.html#createStandardContextMenu
    " popup menu's ownership is transferred to the caller."
    So you must clean up.
    using delete.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #9

      if (menu) is useless, if you didn't had a menu before your application would already have crashed.
      createStandardContextMenu like the name suggests allocates a new QMenu object, hence you have to delete it otherwise you have a memory leak.

      Since you only clear in clearView, why not connect clear directly ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - 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
        #10

        So the final code as per feedback is

        void myTextEdit:contextMenuEvent(QContextMenuEvent event) {
        QMenu * menu = createStandardContextMenu();
        QAction
        clearAction = new IN_CURRENT_POOL QAction("Clear",this);
        connect(clearAction,SIGNAL(triggered()),this ,SLOT(this-clear());
        menu->addAction(clearAction);
        menu->exec(event->globalPos());
        delete menu;
        /// please tell me do we need the line at below
        QTextEdit::contextMenuEvent(QContextMenuEvent* event)
        }

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #11

          SLOT(this->clear()) is wrong please take the time to look at the signals and slots.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - 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

            I will look at signal and slot thanks and we conclude that we do not need

            QTextEdit::contextMenuEvent( event) at the end of the method

            . Since we are using createStandardContextMenu , QMenu * menu = createStandardContextMenu(); and we are not breaking any base class functionality

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

              If I see In QTextEdit the default pop menu on Right Mouse Button click is

              Copy Ctrl+C

              SelectAll Ctrl+A

              I am adding clear to this default pop menu by using following code

              class myTextEdit:: public QTextEdit {
              }

              void myTextEdit:contextMenuEvent(QContextMenuEvent event) {
              QMenu * menu = createStandardContextMenu();
              QAction
              clearAction = new IN_CURRENT_POOL QAction("Clear",this);
              connect(clearAction,SIGNAL(triggered()),this,SLOT(clear()));
              menu->addSeparator();
              menu->addAction(clearAction);
              menu->exec(event->globalPos());
              delete menu;
              }

              I am now getting following pop menu

              Copy Ctrl+C

              SelectAll Ctrl+A

              Clear

              I do not get any Key binding for Clear action . What should be appropriate Key binding for Clear and how to add the same

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

                SelectAll Ctrl+A

                I am adding clear to this default pop menu by using following code

                class myTextEdit: public QTextEdit {
                }

                void myTextEdit:contextMenuEvent(QContextMenuEvent event) {
                QMenu * menu = createStandardContextMenu();
                QAction clearAction = new IN_CURRENT_POOL QAction("Clear",this);
                connect(clearAction,SIGNAL(triggered()),this,SLOT(clear()));
                menu->addSeparator();
                menu->addAction(clearAction);
                menu->exec(event->globalPos());
                delete menu;
                }

                I am now getting following pop menu

                Copy Ctrl+C
                SelectAll Ctrl+A
                Clear

                I do not get any Key binding for Clear action . What should be appropriate Key binding for Clear and how to add the same

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

                  @Qt-Enthusiast said:

                  • What should be appropriate Key binding for Clear and how to add the same

                  Hi
                  You add it to your action
                  clearAction ->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));

                  I dont know what appropriate key would be. One that is not used. :)

                  1 Reply Last reply
                  0
                  • Q Offline
                    Q Offline
                    Qt Enthusiast
                    wrote on last edited by
                    #16
                    This post is deleted!
                    1 Reply Last reply
                    0
                    • Q Offline
                      Q Offline
                      Qt Enthusiast
                      wrote on last edited by
                      #17

                      I am using following code and clear() slot is not getting triggered when the user press Ctrl+R key . Basically nothing happens when I press Ctrl+R Let me know what I am missing in following code

                      void myTextEdit:contextMenuEvent(QContextMenuEvent event) {
                      QMenu * menu = createStandardContextMenu();
                      QAction clearAction = new IN_CURRENT_POOL QAction("Clear",this);
                      clearAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
                      connect(clearAction,SIGNAL(triggered()),this,SLOT(clear()));
                      menu->addSeparator();
                      menu->addAction(clearAction);
                      menu->exec(event->globalPos());
                      delete menu;
                      }

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

                        One more question

                        DO we need to call delete clearAction in the following code or delete menu will take care of all the memory leaks

                        void myTextEdit:contextMenuEvent(QContextMenuEvent event) {
                        QMenu * menu = createStandardContextMenu();
                        QAction clearAction = new IN_CURRENT_POOL QAction("Clear",this);
                        clearAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
                        connect(clearAction,SIGNAL(triggered()),this,SLOT(clear()));
                        menu->addSeparator();
                        menu->addAction(clearAction);
                        menu->exec(event->globalPos());
                        if (clearAction)
                        delete clearAction;

                        delete menu;
                        }

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

                          Hi
                          clearAction is inserted into menu so menu should delete it when
                          you delete menu,

                          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