How to add checkbox to context menu?
-
I'd like to tri-state checkboxes to my context menu.
Here's how I'm trying to do it.I've implemented a class:
class CheckBoxAction(QWidgetAction): def __init__(self, parent, text): super(CheckBoxAction, self).__init__(parent) layout = QHBoxLayout() self.widget = QWidget() label = QLabel(text) label.setAlignment(Qt.AlignLeft) layout.addWidget(QCheckBox()) layout.addWidget(label) self.widget.setLayout(layout) def createWidget(self,text): return self.widget
Then in my context menu I'm adding this widget like so:
menu = QMenu() open = menu.addAction(CheckBoxAction(self,"sometext"))
But instead of showing this checkbox inside of the menu it shows this widget at the top left corner of the screen.
Please advise.
-
I'd like to tri-state checkboxes to my context menu.
Here's how I'm trying to do it.I've implemented a class:
class CheckBoxAction(QWidgetAction): def __init__(self, parent, text): super(CheckBoxAction, self).__init__(parent) layout = QHBoxLayout() self.widget = QWidget() label = QLabel(text) label.setAlignment(Qt.AlignLeft) layout.addWidget(QCheckBox()) layout.addWidget(label) self.widget.setLayout(layout) def createWidget(self,text): return self.widget
Then in my context menu I'm adding this widget like so:
menu = QMenu() open = menu.addAction(CheckBoxAction(self,"sometext"))
But instead of showing this checkbox inside of the menu it shows this widget at the top left corner of the screen.
Please advise.
@midnightdim
You are adding your ownQCheckBox
, widget etc. I don't think you are supposed to do this for aQAction
. You can Google forqt context menu checkbox
, and/or look at https://doc.qt.io/qt-5/qaction.html#checkable-prop.Oohh, are you not able to go this route because of your requirement for tri-state? Then I think https://stackoverflow.com/questions/42846756/how-to-create-tri-state-actions-on-menus-using-qt or https://stackoverflow.com/questions/53812862/tristate-qaction-in-qmenu ?
-
HI
Just as a note. I think a call to setDefaultWidget
might is missing.
https://doc.qt.io/qt-5/qwidgetaction.html#setDefaultWidgetUpdate: ah you have createWidget override. I miss that.
-
@midnightdim
You are adding your ownQCheckBox
, widget etc. I don't think you are supposed to do this for aQAction
. You can Google forqt context menu checkbox
, and/or look at https://doc.qt.io/qt-5/qaction.html#checkable-prop.Oohh, are you not able to go this route because of your requirement for tri-state? Then I think https://stackoverflow.com/questions/42846756/how-to-create-tri-state-actions-on-menus-using-qt or https://stackoverflow.com/questions/53812862/tristate-qaction-in-qmenu ?
@JonB Thanks.
I googled for it of course, and I found the solutions you mention. Note that they inheritQWidgetAction
.
My solution is based on the first one, but I'm not sure if I've ported it to Python correctly.And yes, if I didn't have the requirement for tri-state I could simply use checkable menu actions.
-
@JonB Thanks.
I googled for it of course, and I found the solutions you mention. Note that they inheritQWidgetAction
.
My solution is based on the first one, but I'm not sure if I've ported it to Python correctly.And yes, if I didn't have the requirement for tri-state I could simply use checkable menu actions.
@midnightdim
It helps if you reference where you got your code from when asking the question :)That code uses
setDefaultWidget(_widget);
, which @mrjj called your attention to. So don't you needself.setDefaultWidget(self.widget)
? Hmm, OK, I see he has just changed his comment. Your Python code looks equivalent to the C++ to me. I don't know, unless doing it withsetDefaultWidget()
is better. -
@midnightdim
It helps if you reference where you got your code from when asking the question :)That code uses
setDefaultWidget(_widget);
, which @mrjj called your attention to. So don't you needself.setDefaultWidget(self.widget)
? Hmm, OK, I see he has just changed his comment. Your Python code looks equivalent to the C++ to me. I don't know, unless doing it withsetDefaultWidget()
is better.OH i miss some many thing with python.
in c++ the virtual function is like
https://doc.qt.io/qt-5/qwidgetaction.html#createWidgetQWidgetAction::createWidget(QWidget *parent)
but his is like
def createWidget(self,text):
return self.widgetso extra text with. WIll this still override the base version ? ( i guess not)
-
OH i miss some many thing with python.
in c++ the virtual function is like
https://doc.qt.io/qt-5/qwidgetaction.html#createWidgetQWidgetAction::createWidget(QWidget *parent)
but his is like
def createWidget(self,text):
return self.widgetso extra text with. WIll this still override the base version ? ( i guess not)
@mrjj
Yes, good spot, that does indeed look fluffy. Trouble with Python where you don't actually know whether you're overriding anything :(@midnightdim
(You could put aprint()
statement into your override to see if it's ever really called.) Follow that advice to change your override method. Or, I don't know, use thesetDefaultWidget()
like the C++ does instead. -
@mrjj
Yes, good spot, that does indeed look fluffy. Trouble with Python where you don't actually know whether you're overriding anything :(@midnightdim
(You could put aprint()
statement into your override to see if it's ever really called.) Follow that advice to change your override method. Or, I don't know, use thesetDefaultWidget()
like the C++ does instead.@JonB I actually tested it before, and yes, this override does get called. I'll try to change it to
setDefaultWidget()
. -
@midnightdim
Yes, I did wonder why you chose to adopt different code from that example! :) -
@midnightdim
Yes, I did wonder why you chose to adopt different code from that example! :)@JonB I'm not a pro programmer (started Python less than a year ago, have very small experience in C++) and the documentation for PySide6 doesn't look clear to me, also there were no examples for this particular case. So I wasn't sure about
setDefaultWidget
method in PySide6.
Thanks again. -
Hi,
add checkbox to context menu thorough the video link below
https://www.youtube.com/watch?v=TgDKcdC7X3E -
Hi,
add checkbox to context menu thorough the video link below
https://www.youtube.com/watch?v=TgDKcdC7X3E@danielcharles Why do you post a link to a video handling C#? This is Qt forum...