[Solved] Best practices to hide context menus?



  • Hello,

    are there already any best practices on how to hide some sort of context menu if the user clicks anywhere else in the application?

    I already figured out two approaches which are not sufficient/do not work:

    Create a MouseArea that anchors to the main window and is behind all other elements of the UI. This area emits a signal when it is clicked. The menu connects to this signal and hides itself when it receives the mentioned signal. This approach has the drawback that it only works as long as there are no other MouseAreas on top of this one. So this is not a globally working solution.

    The first solution could be "easily fixed", at least in my opinion, by putting the global MouseArea ON TOP of all other elements. BUT for single clicks the accepted property for mouse events is ignored and the click can not be passed to other areas which renders this idea useless as well.

    So what to do? :)

    I also thought about doing this in C++, maybe I could implement the global mouse-clicked signal there but I did not try this yet since I'd still prefer a QML based solution because I think that this is in fact a very basic requirement for any desktop application (maybe for mobile ones too).

    Kind regards,
    Bernhard



  • I've done something similar to this. I wanted a modal dialog that dismissed when the user clicked anywhere else in the application. I don't know about best practices, but it did work. Take your solution #2 (MouseArea on top of everything else) and set the MouseArea's enabled property to true when the dialog (context menu in your case) is shown, false when the dialog/menu is hidden. This way the MouseArea doesn't eat events that should go to the rest of the GUI when no dialog is shown. Close the dialog when the MouseArea is clicked, and set it to disabled again.



  • Hello,

    omg, thanks a lot, I don't know why I didn't thought about disabling the area myself :)

    Sometimes it just needs someone to open your eyes, I guess. I think I'm getting so used to the fact that many things work somehow tricky in QML that I do not see the obvious ones anymore, hehe.

    Have a nice day and weekend!
    Bernhard

    PS: I still wish that ignoring the "accepted" property would at least be optional, to turn it off for such cases. It's not always practical to "lose" one click.



  • Have you tried explicitly setting accepted to false? I don't know if that would work, but logically it should...

    Glad you got it figured out, though. :) I know the feeling; I spent hours trying to figure out how I could make a scrolling ListView not show list items that have scrolled outside of the view, and finally found that wonderful clip property. Pfft. :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.