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. Advantage of make QShortcut pointer as class variable

Advantage of make QShortcut pointer as class variable

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.3k Views 2 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.
  • Q Offline
    Q Offline
    Qt Enthusiast
    wrote on last edited by
    #1

    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
    {
    Q_OBJECT
    @@ -23,10 +24,11 @@

    protected:
    virtual bool event(QEvent*);

    virtual void contextMenuEvent(QContextMenuEvent*);
    private:
    QTextCharFormat d_format;
    bool d_updating;
    QShortcut* q_shortcut;
    };

    ---------------------------------------------------------------------------------------------myView.cc-----------------------------------------------------------
    #include "myView,moc.h"
    #include <QContextMenuEvent>
    #include <QMenu>
    #include <QShortcut>

    myView::myView(QWidget* dparent) : QTextEdit(dparent), q_shortcut(0)
    {
    setReadOnly(true);
    setLineWrapMode(QTextEdit::NoWrap);
    //// -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
    // 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()));
    ///----------------------------------------------------------------------------------------------------------------------------------------------------------------------

    }

    myView::~myView()
    {
    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();
    menu->addSeparator();
    QAction
    clearAction = new IN_CURRENT_POOL QAction("Clear",this);
    menu->addAction(clearAction);
    clearAction->setShortcut(tr("Ctrl+R"));
    connect(clearAction,SIGNAL(triggered()),this,SLOT(clear()));
    menu->exec(event->globalPos());

    //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

    1 Reply Last reply
    0
    • jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

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

        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

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            Qt Enthusiast
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by jsulm
              #6

              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.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              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