PyQt5 – QAction

  • Post category:Python

PyQt5是基于Qt库的Python开发框架,QAction是PyQt5中的一个类,它可以在菜单栏、工具栏、快捷键等处定义行为和事件,为用户提供更丰富的交互操作。

QAction的基本使用

1. 创建QAction对象

我们可以使用QAction类的构造函数创建一个QAction对象,通常需要传入两个参数:

  • 第一个参数是要显示的文本;
  • 第二个参数是该对象的父控件,可以是MainWindow、QMenu等。
action = QAction('新建', self)

2. 将QAction添加到QMenu或QToolBar中

menu = QMenu()
menu.addAction(action)

toolbar = QToolBar()
toolbar.addAction(action)

3. 调用QAction的信号与相关槽函数连接

action.triggered.connect(self.new_file)

QAction的进阶使用

QAction还有其他一些用法,下面分别介绍:

1. 快捷键

我们可以为QAction设置快捷键,以提高用户的操作效率。快捷键的设置有多种方式,如下所示:

# 通过文本设置快捷键
action.setShortcut('Ctrl+N')

# 通过QKeySequence对象设置快捷键
action.setShortcut(QKeySequence.New)

# QKeySequence中还有其他的快捷键,比如打开文件、保存等
action.setShortcut(QKeySequence.Open)
action.setShortcut(QKeySequence.Save)

2. 图标

我们可以为QAction设置图标,以美化界面。图标的设置有多种方式,如下所示:

# 通过QIcon对象设置图标
action.setIcon(QIcon('new.png'))

# 通过引用Qt库中的图标
action.setIcon(QtGui.QIcon(QtGui.QPixmap(':/images/new.png')))

经过上述设置后,菜单栏和工具栏中的QAction就会显示我们设置的图标了。

示例一:新建文件

示例代码:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QMenu


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

        self.initUI()

    def initUI(self):
        newAction = QAction('新建', self)
        newAction.setShortcut('Ctrl+N')
        newAction.triggered.connect(self.newFile)

        openAction = QAction('打开', self)
        openAction.setShortcut('Ctrl+O')
        openAction.triggered.connect(self.openFile)

        saveAction = QAction('保存', self)
        saveAction.setShortcut('Ctrl+S')
        saveAction.triggered.connect(self.saveFile)

        exitAction = QAction('退出', self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.triggered.connect(self.close)

        fileMenu = self.menuBar().addMenu('文件')
        fileMenu.addAction(newAction)
        fileMenu.addAction(openAction)
        fileMenu.addAction(saveAction)
        fileMenu.addAction(exitAction)

        self.toolBar = self.addToolBar('File')
        self.toolBar.addAction(newAction)
        self.toolBar.addAction(openAction)
        self.toolBar.addAction(saveAction)

        self.show()

    def newFile(self):
        print('新建文件')

    def openFile(self):
        print('打开文件')

    def saveFile(self):
        print('保存文件')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

通过QAction创建了菜单栏和工具栏,同时定义了三个按钮,当按钮被点击时触发相应的函数。

示例二:文本编辑器

示例代码:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QAction, QTextEdit


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

        self.initUI()

    def initUI(self):
        newAction = QAction('新建', self)
        newAction.setShortcut('Ctrl+N')
        newAction.triggered.connect(self.newFile)

        openAction = QAction('打开', self)
        openAction.setShortcut('Ctrl+O')
        openAction.triggered.connect(self.openFile)

        saveAction = QAction('保存', self)
        saveAction.setShortcut('Ctrl+S')
        saveAction.triggered.connect(self.saveFile)

        exitAction = QAction('退出', self)
        exitAction.setShortcut('Ctrl+Q')
        exitAction.triggered.connect(self.close)

        fileMenu = self.menuBar().addMenu('文件')
        fileMenu.addAction(newAction)
        fileMenu.addAction(openAction)
        fileMenu.addAction(saveAction)
        fileMenu.addAction(exitAction)

        self.toolbar = self.addToolBar('File')
        self.toolbar.addAction(newAction)
        self.toolbar.addAction(openAction)
        self.toolbar.addAction(saveAction)

        self.textEdit = QTextEdit(self)
        self.setCentralWidget(self.textEdit)

        self.show()

    def newFile(self):
        self.textEdit.clear()

    def openFile(self):
        fileName, _ = QFileDialog.getOpenFileName(self, '打开文件', '.', '*.txt')
        if fileName:
            with open(fileName, 'r') as f:
                self.textEdit.setText(f.read())

    def saveFile(self):
        fileName, _ = QFileDialog.getSaveFileName(self, '保存文件', '.', '*.txt')
        if fileName:
            with open(fileName, 'w') as f:
                f.write(self.textEdit.toPlainText())


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

通过QAction创建一个带有菜单栏、工具栏和一个文本编辑器的窗口,同时定义了三个按钮,当按钮被点击时会触发相应的函数。其中,打开和保存按钮通过QFileDialog弹出文件对话框,选择文件之后会在文本编辑器中显示或保存。