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. Contextual Toolbar
Forum Updated to NodeBB v4.3 + New Features

Contextual Toolbar

Scheduled Pinned Locked Moved Unsolved Qt for Python
qt for python
8 Posts 3 Posters 973 Views 2 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.
  • U Offline
    U Offline
    unplug
    wrote on last edited by
    #1

    I only have a few weeks of PyQt5 experience so I hope this question is not too trivial. Currently, I am trying to implement something like the contextual toolbar in Word. That is, when table is selected, additional menu AND toolbar hows up. Can anyone provide pointers what widgets I should use? I am trying build a custom widget using QTabBar as a work around but I cannot find out how to control the size of the tab bar. It stretches to fill the width. I cannot figure out how to fix its size. There must be a way because the size of tab bar in QTabWidget is fixed.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      QToolBar is the right tool for that.

      If you are using a QMainWindow, then you have a good part of the mechanic done for you.

      As for "context switching", you will likely want to have the widgets having custom tools to be responsible for providing the their custom tool bars or tool buttons and handle their availability using for example their visibility state.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      U 1 Reply Last reply
      1
      • U Offline
        U Offline
        unplug
        wrote on last edited by
        #3

        @Denni-0 Capture.PNG
        By contextual toolbar I mean the Table Tools in the screen capture, which is visible only then a table is selected.
        Frankly, I am not sure what my customer wants exactly. A use case is like this. An engineering wants to do some analyses and there are different phases. In each phase, there are multiple steps. I imagine the phases will be something like "Table Tools" but permanently visible (or dynamic) and when it is selected, a list of steps in the tool bar appears. Similarly, when phase 2 is selected, a different list of steps will appear in the tool bar.

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          QToolBar is the right tool for that.

          If you are using a QMainWindow, then you have a good part of the mechanic done for you.

          As for "context switching", you will likely want to have the widgets having custom tools to be responsible for providing the their custom tool bars or tool buttons and handle their availability using for example their visibility state.

          U Offline
          U Offline
          unplug
          wrote on last edited by
          #4

          @SGaist
          Initially, I was thinking of using the combination of menu and toolbar. I noticed that when a menu does not have action, it does not appear in the menu bar. Not sure if this is true. I then tried to put everything in the toolbar and put tab bas in it. However, I had trouble to control the toolbar size.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Ok, so something akin to the QtitanRibbon product ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            U 1 Reply Last reply
            0
            • SGaistS SGaist

              Ok, so something akin to the QtitanRibbon product ?

              U Offline
              U Offline
              unplug
              wrote on last edited by
              #6

              @SGaist Thank you. Yes, that's what I was think initially. But since this is a small budget project and I am basically doing this at an undercutting rate so that I have an excuse to learn PyQt, I will try to find something suitable for the purpose. I think the toolbar in Mac should be just fine.
              Screen Shot 2020-04-28 at 4.59.23 PM.png
              In here, Home, Insert, Draw, et. are not part of menu and remain visible the entire time. In my case, it will be Phase 1, Phase 2, etc. And then the buttons will be Step 1, Step 2, etc.

              I think I just need to find the right widget to make a custom toolbar.

              1 Reply Last reply
              0
              • U Offline
                U Offline
                unplug
                wrote on last edited by
                #7

                @Denni-0 Thanks! I will start working on that.

                1 Reply Last reply
                0
                • U Offline
                  U Offline
                  unplug
                  wrote on last edited by unplug
                  #8

                  @Denni-0 Here is what I got. The code is messy because I am wearing too many hats at the moment.

                  from PyQt5.QtWidgets import QMainWindow, QApplication, QLabel, QToolBar, QHBoxLayout, QVBoxLayout, QWidget, QPushButton
                  from PyQt5.QtCore import Qt
                  from PyQt5.Qt import QPalette
                  
                  
                  class MenuToolBar(QWidget):
                      def __init__(self):
                          QWidget.__init__(self)
                          self.menu_layout = QHBoxLayout()
                          self.tool_layout = QHBoxLayout()
                          self.menutool_layout = QVBoxLayout()
                          self.menutool_layout.addLayout(self.menu_layout)
                          self.menutool_layout.addLayout(self.tool_layout)
                          self.setLayout(self.menutool_layout)
                          #
                          # add some menu items
                          menu1 = QPushButton("Phase1")
                          menu1.setFlat(True)
                          menu2 = QPushButton("Phase2")
                          menu2.setFlat(True)
                  
                          self.menus=[]
                          self.menus.append(menu1)
                          self.menus.append(menu2)
                          for i in self.menus:
                              self.menu_layout.addWidget(i)
                          self.menu_layout.addStretch(1)
                          
                          # spacing
                          self.menu_layout.setSpacing(15)
                          self.tool_layout.setSpacing(15)
                  
                          # tools
                          b1 = QPushButton("P1_Step1")
                          b2 = QPushButton("P1_Step2")
                          self.tool_layout.addWidget(b1)
                          self.tool_layout.addWidget(b2)
                          self.tool_layout.addStretch(0)
                  
                          self.menutool_layout.addStretch(1)
                          self.menutool_layout.setSpacing(0)
                          self.setMaximumHeight(100)
                          self.setAutoFillBackground(True)
                          # just to see how big the MenuToolBar is
                          self.setBackgroundRole(QPalette.Highlight)
                  
                          # actions
                          menu2.pressed.connect(self.p2)
                          menu1.pressed.connect(self.p1)
                  
                  
                      def p2(self):
                          for i in reversed(range(self.tool_layout.count())):
                              w = self.tool_layout.takeAt(i).widget()
                              if w is not None:
                                  w.setParent(None)
                          b1 = QPushButton("P2_Step1")
                          b2 = QPushButton("P2_Step2")
                          b3 = QPushButton("P2_Step3")
                  
                          self.tool_layout.addWidget(b1)
                          self.tool_layout.addWidget(b2)
                          self.tool_layout.addWidget(b3)
                          self.tool_layout.addStretch(0)
                  
                      def p1(self):
                          for i in reversed(range(self.tool_layout.count())):
                              w = self.tool_layout.takeAt(i).widget()
                              if w is not None:
                                  w.setParent(None)
                          b1 = QPushButton("P1_Step1")
                          b2 = QPushButton("P1_Step2")
                          self.tool_layout.addWidget(b1)
                          self.tool_layout.addWidget(b2)
                          self.tool_layout.addStretch(0)
                  
                  # driver
                  class MainWindow(QMainWindow):
                      def __init__(self):
                          QMainWindow.__init__(self)
                          self.setFixedSize(600, 500)
                          label = QLabel("App for testing MenuToolBar")
                          label.setAlignment(Qt.AlignCenter)
                          layout = QVBoxLayout()
                          menutoolbar = MenuToolBar()
                          layout.setMenuBar( menutoolbar )
                  
                          layout.addWidget(label)
                          layout.setSpacing(0)
                          w = QWidget()
                          w.setLayout(layout)
                          self.setCentralWidget(w)
                          self.setWindowTitle("MenuToolBar Driver")
                          self.show()
                  
                  if __name__ == "__main__":
                      app = QApplication([])
                      window = MainWindow()
                      app.exec_()
                  
                  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