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)
Qt 6.11 is out! See what's new in the release blog

QTextEdit (clear on right mouse button)

Scheduled Pinned Locked Moved Unsolved General and Desktop
19 Posts 5 Posters 9.2k 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.
  • yuvaramY Offline
    yuvaramY Offline
    yuvaram
    wrote on last edited by
    #2

    Hi ,
    Below can help you
    myTextEdit::myTextEdit(QWidget *parent) : QTextEdit(parent)
    {
    a_action = new QAction("Clear",this);
    connect(a_action,SIGNAL(triggered(bool)),this,SLOT(SLT_clearTextEdit()));
    }

    void myTextEdit:: contextMenuEvent(QContextMenuEvent *event){
    QMenu *menu = createStandardContextMenu();
    menu->addAction(a_action);
    menu->exec();
    }

    void myTextEdit:: SLT_clearTextEdit(){
    this->clear();
    }

    Yuvaram Aligeti
    Embedded Qt Developer
    : )

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

      Hi,

      @yuvaram you are missing delete menu; at the bottom of contextMenuEvent. Right now you have a memory leak.

      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
        #4

        I have written following code

        void myTextEdit:contextMenuEvent(QContextMenuEvent *event) {

        QMenu * menu = createStandardContextMenu();
        QAction* clearAction = new IN_CURRENT_POOL QAction("Clear",this);
        connect(clearAction,SIGNAL(triggered()),this ,SLOT(clearView()));
        menu->addAction(clearAction);
        menu->exec();
        }

        void myTextEdit::clearView() {
        this->clear();
        }

        I am facing only one problem ,placement of pop up now is changed to top left corner ,before this code the pop menu was placed above this QTextEdit and after this code the pop menu was placed at the top left cornet .

        Can some one suggest me what I am missing in my code , so that pop menu should come at above the QTextEdit

        D 1 Reply Last reply
        0
        • Q Qt Enthusiast

          I have written following code

          void myTextEdit:contextMenuEvent(QContextMenuEvent *event) {

          QMenu * menu = createStandardContextMenu();
          QAction* clearAction = new IN_CURRENT_POOL QAction("Clear",this);
          connect(clearAction,SIGNAL(triggered()),this ,SLOT(clearView()));
          menu->addAction(clearAction);
          menu->exec();
          }

          void myTextEdit::clearView() {
          this->clear();
          }

          I am facing only one problem ,placement of pop up now is changed to top left corner ,before this code the pop menu was placed above this QTextEdit and after this code the pop menu was placed at the top left cornet .

          Can some one suggest me what I am missing in my code , so that pop menu should come at above the QTextEdit

          D Offline
          D Offline
          Devopia53
          wrote on last edited by
          #5

          @Qt-Enthusiast

          Try the following:

          menu->exec(event->globalPos());
          delete menu;
          
          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            Qt Enthusiast
            wrote on last edited by
            #6

            1 What is the need of delete menu since no memory is created
            2) In Earlier pop up there is separator line Between Copy and Select All . Currently there is separator between SelecAll and Clear Is there any want we can separator line between Select All and Clear also

            1 Reply Last reply
            0
            • Q Offline
              Q Offline
              Qt Enthusiast
              wrote on last edited by
              #7
              1. I have two question . why there is need to call delete menu , since there is no memory created
              2. do we have to call QTextEdit::contextMenuEvent(QContextMenuEvent *event) at the end of the method

              void myTextEdit:contextMenuEvent(QContextMenuEvent *event) {

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

              }

              void myTextEdit::clearView() {
              this->clear();
              }

              1 Reply Last reply
              0
              • 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