Advantage of make QShortcut pointer as class variable

  • I followed the C++ concept and make q_shortcut as class variable . for following reasons

    As discussed in QT forum , the advantaged of making q_shortcut as class variable because

    1. so that I can clear the memory a in the destruct or
    2. other if required we can enable/disable q_shortcut in other methods of this class

    Following is the code
    ------------------------------------------------ Header is myView.moc.h----------------------------------------------------------------------
    #include <QTextEdit>
    class QContextMenuEvent;
    class QShortcut;
    class myView : public QTextEdit
    @@ -23,10 +24,11 @@

    virtual bool event(QEvent*);

    virtual void contextMenuEvent(QContextMenuEvent*);
    QTextCharFormat d_format;
    bool d_updating;
    QShortcut* q_shortcut;
    #include "myView,moc.h"
    #include <QContextMenuEvent>
    #include <QMenu>
    #include <QShortcut>

    myView::myView(QWidget* dparent) : QTextEdit(dparent), q_shortcut(0)
    //// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
    // short cut key Ctrl+R to clear the Shell
    q_shortcut = new IN_CURRENT_POOL QShortcut(QKeySequence(Qt::CTRL + Qt::Key_R), this);
    connect(q_shortcut, SIGNAL(activated()), this, SLOT(clear()));


    if (q_shortcut) {
    delete q_shortcut;

    // to add Clear sub menu to our existing Right mouse button pop up menu
    // now our Enhanced Pop Menu on Right Mouse button click will be
    Copy Ctrl+C

    Select All Ctrl+A

    Clear Ctrl+R
    void myView::contextMenuEvent(QContextMenuEvent event) {
    QMenu * menu = createStandardContextMenu();
    clearAction = new IN_CURRENT_POOL QAction("Clear",this);

    //Please note:
    //in every call of contextMenuEvent, everytime a new menu is created and
    // returned by createStandardContextMenu .As per qt documentation
    // the popup menu's ownership is transferred to the caller.
    // so it is responsibility of the caller to clear the memory leak
    delete menu;

    Can some one tell me what are advantages and disadvantages of using q_shortcut as class variable in above code

  • Moderators

    The answer depends on your needs.
    If you do not need the pointer later then don't make q_shortcut a class member. Since you pass "this" to the constructor it will be deleted together with myView (so, no need to delete it explicitly).

  • Even if I make it local variable since I pass it to this to constructor then also it will be deleted

    QShortcur* shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_R), this);

    am I correct I my understanding

  • Qt Champions 2017

    @Qt-Enthusiast said:

    QShortcur* shortcut = new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_R), this);

    Yes , it will also be deleted even if local defined.
    The key part is
    shortcut = new QShortcut(QKeySequence(key), this); <<< the "this" give ownership to parent.

  • The only advantage of making it varaible in a class f we want to enable/disable in other methods of same class ( if requirement comes)
    any other advantages you can think it will be helpful

  • Moderators

    I don't really understand why are you looking for advantages.
    It is really easy: you do not need the pointer? Then do not store it in a class variable. Don't do something you do not need. If later you need the pointer you can still change your code to store the pointer in a class variable.

Log in to reply

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