Search code examples
pythonpython-2.7python-3.xpyqtpyqt4

How to have multiple toolbars


I want to have two toolbars on the top but they just stay on each other

#The main toolbar 
self.formatbar = QToolBar() 

self.addToolBar( Qt.TopToolBarArea , self.formatbar )
self.formatbar.addSeparator()
self.formatbar.addWidget(self.fontComboBox)
self.formatbar.addWidget(self.fontSizeComboBox)
self.formatbar.addAction(closeActionTB)
self.formatbar.addAction(openActionTB)
self.formatbar.addAction(saveActionTB)
self.formatbar.addAction(capitalActionTB)
self.formatbar.addAction(smallActionTB)
self.formatbar.addAction(colorActionTB)
self.formatbar.addAction(colorActionBGTB)
self.formatbar.addAction(zoomInActionTB)
self.formatbar.addAction(zoomOutActionTB)
self.formatbar.addAction(changeBoldActionTB)
self.formatbar.addAction(changeItalicActionTB)
self.formatbar.addAction(changeFontUnderlineActionTB)
self.formatbar.addAction(undoActionTB)
self.formatbar.addAction(redoActionTB)
self.formatbar.addAction(magnifyTB)
self.formatbar.addAction(demagnifyTB)
self.formatbar.addAction(printActionTB)
self.formatbar.addAction(findActionTB)
self.formatbar.addAction(alLeftTB)
self.formatbar.addAction(alRightTB)
self.formatbar.addAction(alCenterTB)
self.formatbar.addAction(alJustifyTB)
self.formatbar.addAction(bulletListActionTB)
self.formatbar.addAction(numberListActionTB)

#here the toolbar finishes so i want a new toolbar       
self.formatbar2 = QToolBar()
self.insertToolBar(self.formatbar,self.formatbar2)
self.formatbar2.addAction(indentTB) 
self.formatbar2.addAction(deindentTB)
self.formatbar2.addAction(clearTB)
self.formatbar.addAction(copyRightsTB)

The first picture is how to toolbar is right now and the second picture is how I want it to be

This is how the tool bar is right now

This is how i want i want it(i put it there manually)


Solution

  • According to the docs:

    void QMainWindow::insertToolBar(QToolBar *before, QToolBar *toolbar)

    Inserts the toolbar into the area occupied by the before toolbar so that it appears before it. For example, in normal left-to-right layout operation, this means that toolbar will appear to the left of the toolbar specified by before in a horizontal toolbar area.

    That is to say it places it in the position of before, displacing this, and as we observe that is what it accomplishes but it is not what you expect.

    You must use addToolBarBreak:

    void QMainWindow::addToolBarBreak(Qt::ToolBarArea area = Qt::TopToolBarArea)

    Adds a toolbar break to the given area after all the other objects that are present.

    In your case:

    self.formatbar = QToolBar()
    self.addToolBar( Qt.TopToolBarArea , self.formatbar )
    # add actions and widgets
    self.addToolBarBreak(Qt.TopToolBarArea) # or self.addToolBarBreak()
    self.formatbar2 = QToolBar()
    self.addToolBar( Qt.TopToolBarArea , self.formatbar2)
    # add actions and widgets