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. setContextMenuPolicy not working with QKeySequenceEdit

setContextMenuPolicy not working with QKeySequenceEdit

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 831 Views
  • 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.
  • R Offline
    R Offline
    reonZ
    wrote on 27 Jan 2019, 00:55 last edited by
    #1

    No matter which Qt::ContextMenuPolicy i choose for setContextMenuPolicy of my QKeySequenceEdit, i always get the default windows text edit context menu: alt text

    I tried everything ,Qt::NoContextMenu, Qt::PreventContextMenu, Qt::ActionsContextMenu while adding actions and Qt::CustomContextMenu while using a connect to customContextMenuRequested and nothing changes.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 27 Jan 2019, 01:22 last edited by mrjj
      #2

      Hi
      Yes, it seemed odd at first so i checked the source.
      https://code.woboq.org/qt5/qtbase/src/widgets/widgets/qkeysequenceedit.cpp.html
      Its using a internal LineEdit so thats the one we click on and hence the ContextMenuPolicy
      of the actual QKeySequenceEdit is not really used.

      Knowing this we can do

      KeySequenceEdit *seqEdit = new QKeySequenceEdit(centralWidget());
          QLineEdit *LineEdit = seqEdit->findChild<QLineEdit *>();
          if (LineEdit) {
              LineEdit->setContextMenuPolicy(Qt::CustomContextMenu);
              connect(LineEdit, &QLineEdit::customContextMenuRequested, [this](QPoint pos) {
                  QMenu *menu = new QMenu(this);
                  menu->addAction(new QAction("Action 1", this));
                  menu->addAction(new QAction("Action 2", this));
                  menu->addAction(new QAction("Action 3", this));
                  menu->popup(this->mapToGlobal(pos)); // this might not be excact.
              });
          }
      

      alt text

      Note this is slightly dirty as we use a private widget. ;)

      R 1 Reply Last reply 27 Jan 2019, 01:32
      3
      • M mrjj
        27 Jan 2019, 01:22

        Hi
        Yes, it seemed odd at first so i checked the source.
        https://code.woboq.org/qt5/qtbase/src/widgets/widgets/qkeysequenceedit.cpp.html
        Its using a internal LineEdit so thats the one we click on and hence the ContextMenuPolicy
        of the actual QKeySequenceEdit is not really used.

        Knowing this we can do

        KeySequenceEdit *seqEdit = new QKeySequenceEdit(centralWidget());
            QLineEdit *LineEdit = seqEdit->findChild<QLineEdit *>();
            if (LineEdit) {
                LineEdit->setContextMenuPolicy(Qt::CustomContextMenu);
                connect(LineEdit, &QLineEdit::customContextMenuRequested, [this](QPoint pos) {
                    QMenu *menu = new QMenu(this);
                    menu->addAction(new QAction("Action 1", this));
                    menu->addAction(new QAction("Action 2", this));
                    menu->addAction(new QAction("Action 3", this));
                    menu->popup(this->mapToGlobal(pos)); // this might not be excact.
                });
            }
        

        alt text

        Note this is slightly dirty as we use a private widget. ;)

        R Offline
        R Offline
        reonZ
        wrote on 27 Jan 2019, 01:32 last edited by reonZ
        #3

        @mrjj Arf i knew it would be something like this and i even tried to look for it on my own, sadly i am not experienced enough to go and read sources like you did, thanks really.

        Also what do you mean by Note this is slightly dirty as we use a private widget ?

        And lastly, is there a way for me to add actions to the default context menu ? the one from the screenshot in my OP.

        P.S.: i prefer using Qt::ActionsContextMenu, i don't have to create a menu and connect and the popup is placed automatically at the right position.

        M 1 Reply Last reply 27 Jan 2019, 01:46
        0
        • R reonZ
          27 Jan 2019, 01:32

          @mrjj Arf i knew it would be something like this and i even tried to look for it on my own, sadly i am not experienced enough to go and read sources like you did, thanks really.

          Also what do you mean by Note this is slightly dirty as we use a private widget ?

          And lastly, is there a way for me to add actions to the default context menu ? the one from the screenshot in my OP.

          P.S.: i prefer using Qt::ActionsContextMenu, i don't have to create a menu and connect and the popup is placed automatically at the right position.

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 27 Jan 2019, 01:46 last edited by mrjj
          #4

          @reonZ
          Hi
          Well the wobog browser makes it easy to dig around as all is hot linked.
          So you just go to https://code.woboq.org/qt5/ and type the class name in the search field.

          • Also what do you mean by "Note this is slightly dirty as we use a private widget ?"
            That the LineEdit is not surfaced through any method so we use insider knowledge
            of the class and find a way to access it. if it was ever changed, our code would break.
            But that is very unlikely. :)

          -is there a way for me to add actions to the default context menu ?
          Yes, you can use
          http://doc.qt.io/qt-5/qlineedit.html#createStandardContextMenu
          and then add yours to that.

          R 1 Reply Last reply 27 Jan 2019, 02:03
          3
          • M mrjj
            27 Jan 2019, 01:46

            @reonZ
            Hi
            Well the wobog browser makes it easy to dig around as all is hot linked.
            So you just go to https://code.woboq.org/qt5/ and type the class name in the search field.

            • Also what do you mean by "Note this is slightly dirty as we use a private widget ?"
              That the LineEdit is not surfaced through any method so we use insider knowledge
              of the class and find a way to access it. if it was ever changed, our code would break.
              But that is very unlikely. :)

            -is there a way for me to add actions to the default context menu ?
            Yes, you can use
            http://doc.qt.io/qt-5/qlineedit.html#createStandardContextMenu
            and then add yours to that.

            R Offline
            R Offline
            reonZ
            wrote on 27 Jan 2019, 02:03 last edited by
            #5

            @mrjj Ok thanks for everything, it is working:
            alt text

            1 Reply Last reply
            1

            1/5

            27 Jan 2019, 00:55

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved