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. Multi column context menu
Forum Updated to NodeBB v4.3 + New Features

Multi column context menu

Scheduled Pinned Locked Moved Unsolved Qt for Python
7 Posts 3 Posters 502 Views 1 Watching
  • 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
    buhtz
    wrote on last edited by
    #1

    I would like to have a multi column context menu (or dropdown). I do have 47 items which I am not able to split into multiple sub-menus.

    Something like this:
    alt text

    I do have an (ChatGPT; blame on me!) approach using a QContextMenu with a QTableWidget as custom widget. But the size is a problem.

    Size of context menu and table widget do not fit. Any ideas about it? This should be a 3 columns and 12 rows context menu.
    Bildschirmfoto_2024-11-19_10-34-05.png

    Code:

    from PyQt6.QtWidgets import (QApplication, QMenu, QTableWidget, QTableWidgetItem,
                                  QVBoxLayout, QWidget, QWidgetAction)
    from PyQt6.QtCore import Qt
    
    class TwoColumnContextMenu(QWidget):
        def __init__(self):
            super().__init__()
            self.setWindowTitle("Zwei-Spalten-Kontextmenü")
            self.setGeometry(100, 100, 400, 300)
            self.init_ui()
    
        def init_ui(self):
            # Einfacher Platzhalter für Rechtsklick
            self.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
            self.customContextMenuRequested.connect(self.show_context_menu)
    
        def show_context_menu(self, pos):
            # Benutzerdefiniertes Widget (zwei Spalten)
            table_widget = QTableWidget(12, 3, self)
            for col in [0, 1, 2]:
                for row in range(12):
                    table_widget.setItem(row, col, QTableWidgetItem(f'Option {col}-{row}'))
    
            # Spalten- und Zeilenüberschriften ausblenden
            table_widget.horizontalHeader().setVisible(False)
            table_widget.verticalHeader().setVisible(False)
    
            # Scrollbars entfernen
            table_widget.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
            table_widget.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
    
            # Zellklick-Handler
            table_widget.cellClicked.connect(self.handle_item_click)
    
            # Benutzerdefiniertes Widget in Kontextmenü einfügen
            action_widget = QWidget()
            layout = QVBoxLayout(action_widget)
            layout.addWidget(table_widget)
            action_widget.setLayout(layout)
    
            action = QWidgetAction(self)
            action.setDefaultWidget(action_widget)
    
            # Kontextmenü erstellen
            menu = QMenu(self)
            menu.addAction(action)
    
            table_widget.adjustSize()
            menu.setFixedSize(table_widget.size())
    
            # Menü anzeigen
            menu.exec(self.mapToGlobal(pos))
    
        def handle_item_click(self, row, column):
            item_text = f"Ausgewählt: {row}, {column}"
            print(f"Item angeklickt: {item_text}")
    
    if __name__ == "__main__":
        app = QApplication([])
        window = TwoColumnContextMenu()
        window.show()
        app.exec()
    
    
    jsulmJ 1 Reply Last reply
    0
    • B buhtz

      I would like to have a multi column context menu (or dropdown). I do have 47 items which I am not able to split into multiple sub-menus.

      Something like this:
      alt text

      I do have an (ChatGPT; blame on me!) approach using a QContextMenu with a QTableWidget as custom widget. But the size is a problem.

      Size of context menu and table widget do not fit. Any ideas about it? This should be a 3 columns and 12 rows context menu.
      Bildschirmfoto_2024-11-19_10-34-05.png

      Code:

      from PyQt6.QtWidgets import (QApplication, QMenu, QTableWidget, QTableWidgetItem,
                                    QVBoxLayout, QWidget, QWidgetAction)
      from PyQt6.QtCore import Qt
      
      class TwoColumnContextMenu(QWidget):
          def __init__(self):
              super().__init__()
              self.setWindowTitle("Zwei-Spalten-Kontextmenü")
              self.setGeometry(100, 100, 400, 300)
              self.init_ui()
      
          def init_ui(self):
              # Einfacher Platzhalter für Rechtsklick
              self.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
              self.customContextMenuRequested.connect(self.show_context_menu)
      
          def show_context_menu(self, pos):
              # Benutzerdefiniertes Widget (zwei Spalten)
              table_widget = QTableWidget(12, 3, self)
              for col in [0, 1, 2]:
                  for row in range(12):
                      table_widget.setItem(row, col, QTableWidgetItem(f'Option {col}-{row}'))
      
              # Spalten- und Zeilenüberschriften ausblenden
              table_widget.horizontalHeader().setVisible(False)
              table_widget.verticalHeader().setVisible(False)
      
              # Scrollbars entfernen
              table_widget.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
              table_widget.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
      
              # Zellklick-Handler
              table_widget.cellClicked.connect(self.handle_item_click)
      
              # Benutzerdefiniertes Widget in Kontextmenü einfügen
              action_widget = QWidget()
              layout = QVBoxLayout(action_widget)
              layout.addWidget(table_widget)
              action_widget.setLayout(layout)
      
              action = QWidgetAction(self)
              action.setDefaultWidget(action_widget)
      
              # Kontextmenü erstellen
              menu = QMenu(self)
              menu.addAction(action)
      
              table_widget.adjustSize()
              menu.setFixedSize(table_widget.size())
      
              # Menü anzeigen
              menu.exec(self.mapToGlobal(pos))
      
          def handle_item_click(self, row, column):
              item_text = f"Ausgewählt: {row}, {column}"
              print(f"Item angeklickt: {item_text}")
      
      if __name__ == "__main__":
          app = QApplication([])
          window = TwoColumnContextMenu()
          window.show()
          app.exec()
      
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @buhtz said in Multi column context menu:

      menu.setFixedSize(table_widget.size())

      Why this line?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • B Offline
        B Offline
        buhtz
        wrote on last edited by
        #3

        I tried to make the menu widget the same size as the table widget. But there is no effect.
        Without it the menu widget has always the same size, not reacting to its child table widget.
        If the table is 3x2 the menu is to big. If the table is 3x12 as in my example the widget is to small.

        jsulmJ 1 Reply Last reply
        0
        • B buhtz

          I tried to make the menu widget the same size as the table widget. But there is no effect.
          Without it the menu widget has always the same size, not reacting to its child table widget.
          If the table is 3x2 the menu is to big. If the table is 3x12 as in my example the widget is to small.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @buhtz You're already using layouts, just make sure the table widget expands.
          But why do you use a table at all? Why don't you use a grid layout and add widgets for the actions there?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • B Offline
            B Offline
            buhtz
            wrote on last edited by
            #5

            Nice idea. But the elements in the grid need to react as a menu entry, too.

            jsulmJ 1 Reply Last reply
            0
            • B buhtz

              Nice idea. But the elements in the grid need to react as a menu entry, too.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @buhtz said in Multi column context menu:

              But the elements in the grid need to react as a menu entry

              And what is the problem?
              The widgets in the grid can do whatever you want.

              The cells in the table will also not behave like menu entries out of the box.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • GrecKoG Offline
                GrecKoG Offline
                GrecKo
                Qt Champions 2018
                wrote on last edited by
                #7

                A QTableWidget doesn't seem to be most solution thing for this, you are not presenting data.

                A QHboxLayout of QVBoxLayout would be the solution I'd try.

                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