我想在顶部有两个工具栏,但它们只是彼此重叠
#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)
第一张图片是工具栏现在的样子,第二张图片是我想要的样子
最佳答案
根据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.
也就是说,它将它放置在之前
的位置,取代了它,正如我们所观察到的,这就是它所完成的任务,但它不是您所期望的。
您必须使用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.
就您而言:
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
关于python - 如何拥有多个工具栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45267848/