setContextMenuPolicy not working with QKeySequenceEdit
-
wrote on 27 Jan 2019, 00:55 last edited by
No matter which
Qt::ContextMenuPolicy
i choose forsetContextMenuPolicy
of myQKeySequenceEdit
, i always get the default windows text edit context menu:I tried everything ,
Qt::NoContextMenu
,Qt::PreventContextMenu
,Qt::ActionsContextMenu
while adding actions andQt::CustomContextMenu
while using a connect tocustomContextMenuRequested
and nothing changes. -
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. }); }
Note this is slightly dirty as we use a private widget. ;)
-
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. }); }
Note this is slightly dirty as we use a private widget. ;)
wrote on 27 Jan 2019, 01:32 last edited by reonZ@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. -
@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.@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. - Also what do you mean by "Note this is slightly dirty as we use a private widget ?"
-
@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. - Also what do you mean by "Note this is slightly dirty as we use a private widget ?"
1/5