Home > Blockchain >  How to add an action to the default context menu of a QSpinBox?
How to add an action to the default context menu of a QSpinBox?

Time:02-18

I am using Qt 5.7 (C ) and want to add custom functionality like a reset option to a QSpinBox (as well as QDoubleSpinBox and maybe some other input widgets). This functionality should be accessible via the context menu. However I do not want to replace the default context menu. Instead I want to add my custom actions on top or below the already existing actions.

I found almost matching answers to this question:

However, these do not help in my case since it relies on the Widget to have a method that creates and returns the standard context menu (like QLineEdit::createStandardContextMenu()). The spin boxes do not have such a method.

I also tried to go the cheap way and copy the code that creates the default options directly from source (https://github.com/qt/qtbase/blob/5.7/src/widgets/widgets/qabstractspinbox.cpp line 1249). This is also not really satisfactory since it uses private members of the underlying line edit.

Is there a (standard) way to reuse and augment the default context menu of a Q(Double)SpinBox or any QWidget in general? Or do I have to manually re-implement the default behavior?

CodePudding user response:

https://code.qt.io/cgit/qt/qtbase.git/tree/src/widgets/widgets/qabstractspinbox.cpp#n1315

Yeah it doesn't look like we have any easy "hook" for customizing it (and you can make a feature request if you like); OTOH it's not that much code to copy, since most of the menu entries are added by QLineEdit::createStandardContextMenu()

  • Related