主窗口的多个垂直工具栏
Multiple, Vertical Toolbars for mainwindow
我想创建一个带有两个工具栏的主窗口。第一个应该是水平的,在顶部(经典),第二个应该是垂直的在右侧。
我可以在应用程序运行后移动它们。但是我怎样才能在我的应用程序启动时初始化这个设置呢?
我无法使第二个(垂直)在右侧垂直显示。
当前显示:
所需的显示:
代码:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QMainWindow, QTextEdit, QAction, QApplication, QPushButton, QTableView, QToolBar
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def jump_A(self):
print("Hello A.")
def jump_B(self):
print("Hello B.")
def jump_C(self):
print("Hello C.")
def initUI(self):
# textEdit = QTextEdit()
# self.setCentralWidget(textEdit)
table = QTableView()
self.setCentralWidget(table)
exitAct = QAction(QIcon('system-shutdown.png'), 'Exit', self)
exitAct.setShortcut('Ctrl+Q')
exitAct.setStatusTip('Exit application')
exitAct.triggered.connect(self.close)
AAct = QAction('A', self)
AAct.setShortcut('A')
AAct.setStatusTip('Jump to first entry with "A"')
AAct.triggered.connect(self.jump_A)
BAct = QAction('B', self)
BAct.setShortcut('B')
BAct.setStatusTip('Jump to first entry with "B"')
BAct.triggered.connect(self.jump_B)
CAct = QAction('C', self)
CAct.setShortcut('C')
CAct.setStatusTip('Jump to first entry with "C"')
CAct.triggered.connect(self.jump_C)
self.statusBar()
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(exitAct)
toolbar_main = self.addToolBar('Exit')
toolbar_main.addAction(exitAct)
toolbar_speed_dial = self.addToolBar('SpeedDial')
toolbar_speed_dial.setOrientation(Qt.Vertical)
toolbar_speed_dial.addAction(AAct)
toolbar_speed_dial.addAction(BAct)
toolbar_speed_dial.addAction(CAct)
self.setGeometry(300, 300, 350, 250)
self.setWindowTitle('Main window')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
QMainWindow 有几个 addToolBar() 方法,在您的情况下,您使用的是 addToolBar()
method that passes a string and that by default it will place it on top, if you want it to be placed on the right side you must use the method addToolBar()
that receives Qt::ToolBarArea
and a QToolBar
.
# ...
toolbar_main.addAction(exitAct)
toolbar_speed_dial = QToolBar('SpeedDial')
self.addToolBar(Qt.RightToolBarArea, toolbar_speed_dial)
toolbar_speed_dial.addAction(AAct)
# ...
我想创建一个带有两个工具栏的主窗口。第一个应该是水平的,在顶部(经典),第二个应该是垂直的在右侧。
我可以在应用程序运行后移动它们。但是我怎样才能在我的应用程序启动时初始化这个设置呢? 我无法使第二个(垂直)在右侧垂直显示。
当前显示:
所需的显示:
代码:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QMainWindow, QTextEdit, QAction, QApplication, QPushButton, QTableView, QToolBar
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def jump_A(self):
print("Hello A.")
def jump_B(self):
print("Hello B.")
def jump_C(self):
print("Hello C.")
def initUI(self):
# textEdit = QTextEdit()
# self.setCentralWidget(textEdit)
table = QTableView()
self.setCentralWidget(table)
exitAct = QAction(QIcon('system-shutdown.png'), 'Exit', self)
exitAct.setShortcut('Ctrl+Q')
exitAct.setStatusTip('Exit application')
exitAct.triggered.connect(self.close)
AAct = QAction('A', self)
AAct.setShortcut('A')
AAct.setStatusTip('Jump to first entry with "A"')
AAct.triggered.connect(self.jump_A)
BAct = QAction('B', self)
BAct.setShortcut('B')
BAct.setStatusTip('Jump to first entry with "B"')
BAct.triggered.connect(self.jump_B)
CAct = QAction('C', self)
CAct.setShortcut('C')
CAct.setStatusTip('Jump to first entry with "C"')
CAct.triggered.connect(self.jump_C)
self.statusBar()
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(exitAct)
toolbar_main = self.addToolBar('Exit')
toolbar_main.addAction(exitAct)
toolbar_speed_dial = self.addToolBar('SpeedDial')
toolbar_speed_dial.setOrientation(Qt.Vertical)
toolbar_speed_dial.addAction(AAct)
toolbar_speed_dial.addAction(BAct)
toolbar_speed_dial.addAction(CAct)
self.setGeometry(300, 300, 350, 250)
self.setWindowTitle('Main window')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
QMainWindow 有几个 addToolBar() 方法,在您的情况下,您使用的是 addToolBar()
method that passes a string and that by default it will place it on top, if you want it to be placed on the right side you must use the method addToolBar()
that receives Qt::ToolBarArea
and a QToolBar
.
# ...
toolbar_main.addAction(exitAct)
toolbar_speed_dial = QToolBar('SpeedDial')
self.addToolBar(Qt.RightToolBarArea, toolbar_speed_dial)
toolbar_speed_dial.addAction(AAct)
# ...