How to keep QMenu context menu inside my mainscreen?
-
I have created a context menu.
+Main 1 > Sub 1.1 > Sub 1.1.1
+Main 1 > Sub 1.1 > Sub 1.1.2
+Main 2The context menu is going to be shown by a click on a list.
The problem is, when I clicked near the border of mainscreen, some part of the sub-menus' stays outside the screen.
To solve this issue, I tried to show sub-menus in a moved position but I have failed.
It moved but crashed the software...Here is my creater method:
def rightClickMenu_create(self): self.context_menu = QMenu(self) self.item1 = self.context_menu.addMenu("Main 1") item2 = self.context_menu.addAction("Main 2") self.item1_1 = self.item1.addMenu("Sub 1.1") self.item1_1.addAction("Sub-sub 1.1.1") self.item1_1.addAction("Sub-sub 1.1.2") self.item1.aboutToShow.connect(self.show_sub_menu_at_position)
I can show the menu with;
def show_main_menu(self): self.context_menu.exec_(QCursor.pos())
I want to move the sub-menu with;
def show_sub_menu_at_position(self): self.item1.aboutToShow.disconnect(self.show_sub_menu_at_position) self.item1.exec_(self.mapToGlobal(QPoint(50, 50)))
How can I move the sub-menu to another coordinate or how to keep my context-menu inside the borders?
If the menu opened on the right side of the main window, the sub-menus must be opened to right, if on the left border must be opened to right, if on top must open to bottom, if on bottom must be opened to top....
-
@bladekel said in How to keep QMenu context menu inside my mainscreen?:
It moved but crashed the software...
That will doubtless come from going
self.item1.aboutToShow.disconnect(self.show_sub_menu_at_position)
while you are inside a slot which has been invoked via the signal. Don't disconnect signals/slots inside code which is being executed as that signal/slot, bad things will happen back in Qt infrastructure upon return I suspect!I don't know about all your positioning for the actual problem. I would have thought menus or sub-menus would "arrange themselves" (either OS window code or Qt) so that they don't spill off the visible area, but I don't know. Might depend what platform/windowing system you are using.
-
def show_sub_menu_at_position(self): if sistem.isContextShown == 0: sistem.isContextShown = 1 self.item1.exec_(self.mapToGlobal(QPoint(50, 50)))
I have changed the method to run for one-time operation, but again failed...
The sub-menu moved to 50,50 but after I clicked to another place to hide the context, it crashed...