QTextEdit (clear on right mouse button)
-
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
inclearView
, 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)
} -
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
ClearI do not get any Key binding for Clear action . What should be appropriate Key binding for Clear and how to add the same
-
@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;
} -
Hi
clearAction is inserted into menu so menu should delete it when
you delete menu,