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.
You are adding your own
QCheckBox, widget etc. I don't think you are supposed to do this for a
QAction. You can Google for
qt 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 ?
Just as a note. I think a call to setDefaultWidget
might is missing.
Update: ah you have createWidget override. I miss that.
I googled for it of course, and I found the solutions you mention. Note that they inherit
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.
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 need
self.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 with
OH i miss some many thing with python.
in c++ the virtual function is like
but his is like
so extra text with. WIll this still override the base version ? ( i guess not)
Yes, good spot, that does indeed look fluffy. Trouble with Python where you don't actually know whether you're overriding anything :(
(You could put a
print()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 the
setDefaultWidget()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
JonB last edited by
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
setDefaultWidgetmethod in PySide6.
danielcharles last edited by
add checkbox to context menu thorough the video link below
@danielcharles Why do you post a link to a video handling C#? This is Qt forum...