Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. How to keep QMenu context menu inside my mainscreen?
Forum Updated to NodeBB v4.3 + New Features

How to keep QMenu context menu inside my mainscreen?

Scheduled Pinned Locked Moved Unsolved Qt for Python
3 Posts 2 Posters 248 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bladekel
    wrote on last edited by
    #1

    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 2

    The 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....

    JonBJ 1 Reply Last reply
    0
    • B bladekel

      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 2

      The 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....

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @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.

      B 1 Reply Last reply
      0
      • JonBJ JonB

        @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.

        B Offline
        B Offline
        bladekel
        wrote on last edited by
        #3

        @JonB

        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...

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved