QTextEdit (clear on right mouse button)



  • Hi All

    I have a class as class
    myLogView : public QTextEdit {

    }

    When I do Right mouse button click , I get Copy and SelectAll as two actions (menus) . I suppose they are by deafault by QTextEdit . Is there any way I can add additional action call Clear (on right mouse button) , When the user clicks on Clear menu (action) it should clear the QTextEdit widget



  • 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();
    }


  • Lifetime Qt Champion

    Hi,

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



  • 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



  • @Qt-Enthusiast

    Try the following:

    menu->exec(event->globalPos());
    delete menu;
    


  • 1 What is the need of delete menu since no memory is created

    1. 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. 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();
    }


  • Qt Champions 2016

    @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.


  • Lifetime Qt Champion

    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 ?



  • 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)
    }


  • Lifetime Qt Champion

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



  • 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



  • 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



  • 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


  • Qt Champions 2016

    @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. :)



  • This post is deleted!


  • 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;
    }



  • 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;
    }


  • Qt Champions 2016

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.