Why we delete menu in custmosied contextMenuEvent
-
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
-
@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.
-
but in QT as I know the children objects are deleted automatically because of parent child relation
-
@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. -
@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. -
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
-
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
-
@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();
-
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();
} -
@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.
-
@Qt-Enthusiast What do you expect to happen if you add a destructor which does not do anything? So, what is your question about?
-
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 ?
-
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. -
@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?).