Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Contextual Toolbar



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


  • Lifetime Qt Champion

    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.


  • Banned

    @unplug I can probably help you with this if I can get a better idea of what it is you are striving to create -- sorry not exactly sure what you mean by a "contextual toolbar like in MS Word". If you have an image of this I can most likely help you translate this into a Python-Qt version.



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



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


  • Banned

    Typically (and/or traditionally) anything found in your ToolBar is a quick button (short cut) to something that exists within your MenuBar -- rarely do you have something appear in a ToolBar that is not found within the MenuBar system somewhere -- but there are exceptions to every guideline ;)

    Okay I will roll this around in my head and get back to you tomorrow -- time for me to head home


  • Lifetime Qt Champion

    Ok, so something akin to the QtitanRibbon product ?


  • Banned

    Okay when sub-classing the MenuToolBar I use a QDockWidget and this gets you the standard menu and/or tool bar but you are wanting to expand upon the generic version of this so you can use a third party product (which makes you dependent on someone else's implementation or simply make one of your own by subclassing the QMainWindow since this is what defines and restricts the menu and/or sub-classing a specialized QDockWidget that expands how the QMainWindow.MenuBar is defined.

    These are suggestions and pointers to where you might start looking into how to create what you are asking about but I have not done that myself as of yet. However if you want to work this out together here I am willing. If so I would suggest you make a simple QMainWindow with a simple MenuBar that has maybe 3 items with 1 selection per item this will give us a basic MUC we can then begin to build upon. Once you have that made post it here and we will give you feed back and see what we can do together



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



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



  • @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_()
    

Log in to reply