多个垂直的主风工具栏

2024-09-27 20:17:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我想创建一个有两个工具栏的主窗口。第一个应该是水平的,在顶部(经典),第二个垂直在右侧。在

一旦应用程序运行,我就可以移动它们。但如何在我的应用程序启动时初始化此设置? 我不能让第二个(垂直)垂直显示在右侧。在

当前显示:

enter image description here

所需显示:

enter image description here

代码:

#!/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_())

Tags: importselfdefspeedtoolbarjumpaddactionqaction
1条回答
网友
1楼 · 发布于 2024-09-27 20:17:36

QMainWindow有几个addToolBar()方法,在您的例子中,您使用的是传递一个字符串的^{}方法,默认情况下,它将把它放在上面;如果您想将它放在右侧,则必须使用接收^{}^{}的方法^{}。在

# ...
toolbar_main.addAction(exitAct)

toolbar_speed_dial = QToolBar('SpeedDial')
self.addToolBar(Qt.RightToolBarArea, toolbar_speed_dial)

toolbar_speed_dial.addAction(AAct)
# ...

enter image description here

相关问题 更多 >

    热门问题