前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PySide6 GUI 编程(22):QToolBar的简单用法

PySide6 GUI 编程(22):QToolBar的简单用法

原创
作者头像
bowenerchen
发布2024-07-31 15:51:11
1650
发布2024-07-31 15:51:11
举报
文章被收录于专栏:编码视界

基础用法

工具栏的样式

示例代码

代码语言:python
代码运行次数:0
复制
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QApplication, QLabel, QMainWindow, QPushButton, QToolBar


class MyToolBar(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle('ToolBar Demo')
        self.label = QLabel('Hello, ToolBar')
        self.label.setAlignment(Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter)
        self.setCentralWidget(self.label)
        self.tool_bar = QToolBar('MyToolBar')
        self.tool_bar.setToolTip('this is my tool-bar')
        self.tool_bar.addWidget(QPushButton('ToolButton'))
        self.tool_bar.toggleViewAction().setEnabled(False) # 当设置为 False 时,鼠标右键不能关闭工具栏
        self.addToolBar(self.tool_bar)


if __name__ == '__main__':
    app = QApplication()
    ins = MyToolBar()
    ins.show()
    app.exec()

运行效果

静态展示效果
静态展示效果
工具栏拖动效果
工具栏拖动效果

关闭工具栏

设置 toggleViewAction 为 True
设置 toggleViewAction 为 True

运行效果

关闭工具栏
关闭工具栏

使用 QStatusBar 显示状态

示例代码

代码语言:python
代码运行次数:0
复制
from datetime import datetime

from PySide6.QtCore import Qt
from PySide6.QtGui import QAction
from PySide6.QtWidgets import QApplication, QLabel, QMainWindow, QStatusBar, QToolBar


def action_button_toggled(state: bool):
    print('action button toggled to:{} @{}'.format(state, datetime.now().isoformat(sep = ' ')))


class MyToolBar(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle('ToolBar Demo')
        self.label = QLabel('Hello, ToolBar')
        self.label.setAlignment(Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter)
        self.setCentralWidget(self.label)
        self.tool_bar = QToolBar('MyToolBar')
        self.tool_bar.setToolTip('this is my tool-bar')
        self.tool_bar.toggleViewAction().setEnabled(False)  # 当设置为 False 时,鼠标右键不能关闭工具栏

        # 创建一个 QAction,设置父对象为当前窗口
        # 当parent控件被销毁时,它的子控件也会被自动销毁
        # 这有助于防止内存泄漏和资源管理问题
        self.action_button = QAction('ToolBarActionButton', self)
        self.action_button.setToolTip('this is my toolbar action button')
        self.action_button.setStatusTip('action button status tip')
        self.action_button.setCheckable(True)
        self.action_button.triggered.connect(self.action_button_triggered)
        self.action_button.toggled.connect(action_button_toggled)
        self.tool_bar.addAction(self.action_button)

        # 创建一个 QStatusBar,设置父对象为当前窗口
        # 当parent控件被销毁时,它的子控件也会被自动销毁
        # 这有助于防止内存泄漏和资源管理问题
        self.status_bar = QStatusBar(self)
        self.setStatusBar(self.status_bar)

        self.addToolBar(self.tool_bar)

    def action_button_triggered(self):
        #  ISO 8601 格式时间
        self.status_bar.showMessage(
            f"action-button triggered@{datetime.now().isoformat(sep = ' ')}")


if __name__ == '__main__':
    app = QApplication()
    ins = MyToolBar()
    ins.show()
    app.exec()

运行效果

工具栏结合状态栏展示
工具栏结合状态栏展示

设置工具栏图标

示例代码

代码语言:python
代码运行次数:0
复制
from PySide6.QtCore import QSize, Qt
from PySide6.QtGui import QAction, QIcon
from PySide6.QtWidgets import QApplication, QLabel, QMainWindow, QToolBar


class MyToolBar(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle('ToolBar Demo')
        self.label = QLabel('Hello, ToolBar')
        self.label.setAlignment(Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter)

        self.setCentralWidget(self.label)
        self.tool_bar = QToolBar('MyToolBar')
        self.tool_bar.setToolTip('this is my tool-bar')
        self.tool_bar.toggleViewAction().setEnabled(False)  # 当设置为 False 时,鼠标右键不能关闭工具栏
        self.tool_bar.setIconSize(QSize(64, 64))
        # Qt uses your operating system default settings to determine
        # whether to show an icon, text or an icon and text in the toolbar
        # But you can override this by using.setToolButtonStyle
        # 此工具栏显式指定样式
        self.tool_bar.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextUnderIcon)

        # 创建一个 QAction,设置父对象为当前窗口
        # 当parent控件被销毁时,它的子控件也会被自动销毁
        # 这有助于防止内存泄漏和资源管理问题
        self.action_button = QAction(
            QIcon('xxxxxxx/xxxxxxx/xxxxxxx.png'),
            'ToolBarActionButton',
            self)
        self.action_button.setToolTip('this is my toolbar action button')
        self.action_button.setStatusTip('action button status tip')
        self.action_button.setCheckable(True)
        self.tool_bar.addAction(self.action_button)
        self.addToolBar(self.tool_bar)

        # 此工具栏不显式指定样式
        self.tool_bar_2 = QToolBar('AnotherToolBar')
        self.tool_bar_2.toggleViewAction().setEnabled(False)
        self.tool_bar_2.setIconSize(QSize(64, 64))
        self.action_button_2 = QAction(
            QIcon('xxxxxxx/xxxxxxx/xxxxxxx.png'),
            'AnotherToolBarActionButton',
            self)
        self.action_button_2.setToolTip('this is my ANOTHER toolbar action button')
        self.action_button_2.setStatusTip('action button status tip')
        self.action_button_2.setCheckable(True)
        self.tool_bar_2.addAction(self.action_button_2)
        self.addToolBar(self.tool_bar_2)

        # 如果工具栏自身没有显式指定,则使用这种样式
        self.setToolButtonStyle(Qt.ToolButtonStyle.ToolButtonTextBesideIcon)


if __name__ == '__main__':
    app = QApplication()
    ins = MyToolBar()
    ins.show()
    app.exec()

展示效果

两种图标展示样式
两种图标展示样式

toggleViewAction 被关闭
toggleViewAction 被关闭

鼠标悬浮时展示的 Action Button Tip 信息
鼠标悬浮时展示的 Action Button Tip 信息

图标按钮点击效果
图标按钮点击效果

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 基础用法
    • 工具栏的样式
      • 示例代码
      • 运行效果
    • 关闭工具栏
      • 运行效果
  • 使用 QStatusBar 显示状态
    • 示例代码
      • 运行效果
      • 设置工具栏图标
        • 示例代码
          • 展示效果
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档