Solved QMenu signal aboutToShow()
-
Can I open the menu after checking it using aboutToShow?
-
@Qt-Jo-Ha What do you mean? The menu will be shown after your slot connected to aboutToShow() was executed. Please explain better what you want to do.
-
Before opening the menu, I want to check one value, and if it is not true, a message box will appear and the menu will not open
Thanks for the answer
-
@Qt-Jo-Ha I don't think you can do this using aboutToShow().
You should rethink your design: if a menu is not disabled then it should work, else user will be irritated why it is not working.
So, you should better disable that menu if the condition is not fulfilled. -
Thanks for the answer and I have 2 questions
-
Is the way to explain the reason why it can't be used with a message box?
-
If the menu button is active, is there any way to prevent it from opening when clicked?
-
-
@Qt-Jo-Ha What should actually happen if the user clicks on the menu and the condition is true? I guess you have a slot connected to the menu? You could simply check the condition there and decide what to do.
-
@jsulm said in QMenu signal aboutToShow():
What should actually happen if the user clicks on the menu and the condition is true? I guess you have a slot connected to the menu? You could simply check the condition there and decide what to do.
I just want to open a menu if the condition is true when clicked. However, if the condition is false, it tries to open a message box without opening the menu.
-
@Qt-Jo-Ha What if you call https://doc.qt.io/qt-5/qwidget.html#setDisabled (with true) inside aboutToShow() if condition is false?
-
Calling setDisable inside aboutToShow() also opens the menu.
-
@Qt-Jo-Ha
I would not recommend this approach. But if you do want to persist, andaboutToShow()
is too late, what about disabling during anotherQMenu
event likehovered()
ormousePressedEvent()
etc.? Or, what about (perhaps creating a subclass which) interceptsQMenu::triggered
orQAction::triggered
and does your work there, only passing the signal onto your original action triggered slot if the condition passes? Something along these lines, depending on exactly where you wish to catch the user's action.