效果图:

按照上一节的方法,将剪切-复制-粘贴图标放置到工具栏后,为其指定槽函数。这些功能无需自己编写代码来实现,QPlainTextEdit提供了实现这些编辑功能的槽函数,如cut()、copy()、paste()等,只需要将这些Action和相应的槽函数关联即可。

然后将ui转换成py文件,执行即可。

ui_mainWindow.py

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'ui_mainWindow.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(532, 367)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.plainTextEdit = QtWidgets.QPlainTextEdit(self.centralwidget)
self.plainTextEdit.setGeometry(QtCore.QRect(0, 0, 531, 291))
font = QtGui.QFont()
font.setPointSize(18)
self.plainTextEdit.setFont(font)
self.plainTextEdit.setObjectName("plainTextEdit")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 532, 21))
self.menubar.setObjectName("menubar")
self.menu = QtWidgets.QMenu(self.menubar)
self.menu.setObjectName("menu")
self.menu_2 = QtWidgets.QMenu(self.menubar)
self.menu_2.setObjectName("menu_2")
self.menu_3 = QtWidgets.QMenu(self.menubar)
self.menu_3.setObjectName("menu_3")
MainWindow.setMenuBar(self.menubar)
self.toolBar = QtWidgets.QToolBar(MainWindow)
self.toolBar.setToolButtonStyle(QtCore.Qt.ToolButtonIconOnly)
self.toolBar.setObjectName("toolBar")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar)
self.actFont_Italic = QtWidgets.QAction(MainWindow)
self.actFont_Italic.setCheckable(True)
icon = QtGui.QIcon()
icon.addPixmap(QtGui.QPixmap("icons/images/Italic.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actFont_Italic.setIcon(icon)
self.actFont_Italic.setObjectName("actFont_Italic")
self.actFont_Bold = QtWidgets.QAction(MainWindow)
self.actFont_Bold.setCheckable(True)
icon1 = QtGui.QIcon()
icon1.addPixmap(QtGui.QPixmap("icons/images/Bold.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actFont_Bold.setIcon(icon1)
self.actFont_Bold.setObjectName("actFont_Bold")
self.actFont_Underline = QtWidgets.QAction(MainWindow)
self.actFont_Underline.setCheckable(True)
icon2 = QtGui.QIcon()
icon2.addPixmap(QtGui.QPixmap("icons/images/underline.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actFont_Underline.setIcon(icon2)
self.actFont_Underline.setObjectName("actFont_Underline")
self.actEdit_Cut = QtWidgets.QAction(MainWindow)
icon3 = QtGui.QIcon()
icon3.addPixmap(QtGui.QPixmap("icons/images/cut.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actEdit_Cut.setIcon(icon3)
self.actEdit_Cut.setObjectName("actEdit_Cut")
self.actEdit_Copy = QtWidgets.QAction(MainWindow)
icon4 = QtGui.QIcon()
icon4.addPixmap(QtGui.QPixmap("icons/images/copy.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actEdit_Copy.setIcon(icon4)
self.actEdit_Copy.setObjectName("actEdit_Copy")
self.actEdit_Paste = QtWidgets.QAction(MainWindow)
icon5 = QtGui.QIcon()
icon5.addPixmap(QtGui.QPixmap("icons/images/paste.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
self.actEdit_Paste.setIcon(icon5)
self.actEdit_Paste.setObjectName("actEdit_Paste")
self.menubar.addAction(self.menu.menuAction())
self.menubar.addAction(self.menu_2.menuAction())
self.menubar.addAction(self.menu_3.menuAction())
self.toolBar.addAction(self.actFont_Italic)
self.toolBar.addSeparator()
self.toolBar.addAction(self.actFont_Bold)
self.toolBar.addSeparator()
self.toolBar.addAction(self.actFont_Underline)
self.toolBar.addSeparator()
self.toolBar.addAction(self.actEdit_Cut)
self.toolBar.addAction(self.actEdit_Copy)
self.toolBar.addAction(self.actEdit_Paste)
self.toolBar.addSeparator() self.retranslateUi(MainWindow)
self.actEdit_Cut.triggered.connect(self.plainTextEdit.cut)
self.actEdit_Copy.triggered.connect(self.plainTextEdit.copy)
self.actEdit_Paste.triggered.connect(self.plainTextEdit.paste)
QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.menu.setTitle(_translate("MainWindow", "文件(F)"))
self.menu_2.setTitle(_translate("MainWindow", "编辑(E)"))
self.menu_3.setTitle(_translate("MainWindow", "格式(M)"))
self.toolBar.setWindowTitle(_translate("MainWindow", "toolBar"))
self.actFont_Italic.setText(_translate("MainWindow", "斜体"))
self.actFont_Italic.setToolTip(_translate("MainWindow", "<html><head/><body><p><span style=\" font-style:italic;\">斜体</span></p></body></html>"))
self.actFont_Bold.setText(_translate("MainWindow", "粗体"))
self.actFont_Bold.setToolTip(_translate("MainWindow", "加粗"))
self.actFont_Underline.setText(_translate("MainWindow", "下划线"))
self.actFont_Underline.setToolTip(_translate("MainWindow", "下划线"))
self.actEdit_Cut.setText(_translate("MainWindow", "剪切"))
self.actEdit_Cut.setToolTip(_translate("MainWindow", "剪切到粘贴板"))
self.actEdit_Cut.setShortcut(_translate("MainWindow", "Ctrl+X"))
self.actEdit_Copy.setText(_translate("MainWindow", "复制"))
self.actEdit_Copy.setToolTip(_translate("MainWindow", "复制到粘贴板"))
self.actEdit_Copy.setShortcut(_translate("MainWindow", "Ctrl+C"))
self.actEdit_Paste.setText(_translate("MainWindow", "粘贴"))
self.actEdit_Paste.setToolTip(_translate("MainWindow", "从粘贴板粘贴"))
self.actEdit_Paste.setShortcut(_translate("MainWindow", "Ctrl+V"))

myMainWindow.py

#!/usr/bin/env python
# _*_ coding: UTF-8 _*_
"""=================================================
@Project -> File : Operate-system -> myMainWindow.py
@IDE : PyCharm
@Author : zihan
@Date : 2020/4/11 14:44
@Desc :
=================================================""" import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QActionGroup, QLabel, QProgressBar, QSpinBox, QFontComboBox)
from PyQt5.QtCore import Qt, pyqtSlot
from PyQt5.QtGui import QTextCharFormat, QFont from ui_mainWindow import Ui_MainWindow class QmyMainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self) # 设置斜体
self.ui.actFont_Italic.triggered.connect(self.do_act_font_italic_triggered)
# 设置粗体
self.ui.actFont_Bold.triggered.connect(self.do_act_font_bold_triggered)
# 设置下划线
self.ui.actFont_Underline.triggered.connect(self.do_act_underline_triggered) @pyqtSlot(bool)
def do_act_font_italic_triggered(self, checked): # 斜体
fmt = self.ui.plainTextEdit.currentCharFormat()
fmt.setFontItalic(checked)
self.ui.plainTextEdit.mergeCurrentCharFormat(fmt) @pyqtSlot(bool)
def do_act_font_bold_triggered(self, checked): # 粗体
fmt = self.ui.plainTextEdit.currentCharFormat()
if checked:
fmt.setFontWeight(QFont.Bold)
else:
fmt.setFontWeight(QFont.Normal)
self.ui.plainTextEdit.mergeCurrentCharFormat(fmt) @pyqtSlot(bool)
def do_act_underline_triggered(self, checked): # 下划线
fmt = self.ui.plainTextEdit.currentCharFormat()
fmt.setFontUnderline(checked)
self.ui.plainTextEdit.mergeCurrentCharFormat(fmt) if __name__ == "__main__":
app = QApplication(sys.argv) # 创建app,用QApplication类
form = QmyMainWindow()
form.show()
sys.exit(app.exec_())

ok.

第十二篇 -- QMainWindow与QAction(剪切-复制-粘贴)的更多相关文章

  1. 第十四篇 -- QMainWindow与QAction(清空-全选-撤销-重做-关闭-语言选择)

    效果图: 这次添加了关闭-撤销-重做-全选-清空等功能,并添加了字体和字体大小选择.基本方法跟前面几篇类似. ui_mainWindow.py # -*- coding: utf-8 -*- # Fo ...

  2. 解剖SQLSERVER 第十二篇 OrcaMDF 行压缩支持(译)

    解剖SQLSERVER 第十二篇   OrcaMDF 行压缩支持(译) http://improve.dk/orcamdf-row-compression-support/ 在这两个月的断断续续的开发 ...

  3. 第十二篇 SQL Server代理多服务器管理

    本篇文章是SQL Server代理系列的第十二篇,详细内容请参考原文 在这一系列的上一篇,我们查看了维护计划,一个维护计划可能会创建多个作业,多个计划.你还简单地看了SSIS子系统,并查看了维护计划作 ...

  4. 第十二篇 Integration Services:高级日志记录

    本篇文章是Integration Services系列的第十二篇,详细内容请参考原文. 简介在前一篇文章我们配置了SSIS内置日志记录,演示了简单和高级日志配置,保存并查看日志配置,生成自定义日志消息 ...

  5. Python之路【第十二篇】:JavaScrpt -暂无内容-待更新

    Python之路[第十二篇]:JavaScrpt -暂无内容-待更新

  6. Python开发【第二十二篇】:Web框架之Django【进阶】

    Python开发[第二十二篇]:Web框架之Django[进阶]   猛击这里:http://www.cnblogs.com/wupeiqi/articles/5246483.html 博客园 首页 ...

  7. 【译】第十二篇 Integration Services:高级日志记录

    本篇文章是Integration Services系列的第十二篇,详细内容请参考原文. 简介在前一篇文章我们配置了SSIS内置日志记录,演示了简单和高级日志配置,保存并查看日志配置,生成自定义日志消息 ...

  8. 【译】第十二篇 SQL Server代理多服务器管理

    本篇文章是SQL Server代理系列的第十二篇,详细内容请参考原文 在这一系列的上一篇,我们查看了维护计划,一个维护计划可能会创建多个作业,多个计划.你还简单地看了SSIS子系统,并查看了维护计划作 ...

  9. 跟我学SpringCloud | 第十二篇:Spring Cloud Gateway初探

    SpringCloud系列教程 | 第十二篇:Spring Cloud Gateway初探 Springboot: 2.1.6.RELEASE SpringCloud: Greenwich.SR1 如 ...

随机推荐

  1. 即时性能分析工具 Pyroscope

    当网站上线后,流量增加或短暂功能故障,都会造成使用者体验相当不好,而这时该怎么快速找到性能的瓶颈呢?通常 CPU 达到 100% 时,有时候也很难复制及找出关键问题点. 本篇文章,我们会介绍一套工具叫 ...

  2. fiddler操作详情

    1.设置fiddler请求过滤 2.请求与响应的格式内容  3.拦截请求操作 a.按F11开始拦截,发送请求 b.修改请求数据 c.SHIFT+F11关闭拦截 d.run to complete,把修 ...

  3. [翻译]Go与C#的比较,第二篇:垃圾回收

    Go vs C#, part 2: Garbage Collection | by Alex Yakunin | ServiceTitan - Titan Tech | Medium 目录 译者注 什 ...

  4. csp-s模拟测试57(10.2)「天空龙」·「巨神兵」·「太阳神」

    题目是古埃及神话??? A. 天空龙 傻逼模拟,看来没有滑天下之大稽QAQ,也没有打错快读(大雾...) B. 巨神兵 难度爆增,一脸懵比..... 60分状压: 因为是求有向图,关于有向图好像拓扑用 ...

  5. 【题解】codeforces 467C George and Job dp

    题目描述 新款手机 iTone6 近期上市,George 很想买一只.不幸地,George 没有足够的钱,所以 George 打算当一名程序猿去打工.现在George遇到了一个问题. 给出一组有 n ...

  6. 解决SpringMVC重复提交的问题

    方法一:通过重定向采取请求转发的方式完成表单内容的添加会造成内容的重复插入.当向Servlet发送一条增加记录的请求后,servlet首先向数据库增加一条记录,然后又从数据库中查询出所有数据,接着转发 ...

  7. vscode中html和vue没有自动补全,需要怎么配置

    先安装HTML Snippets插件 点击 文件-首选项-设置,然后根据以下操作 然后在setting.json中加入以下代码 然后就有提示了

  8. 32、JavaScript介绍

    32.1.JavaScript概述: 1.JavaScript的历史: 1992年Nombas开发出C-minus-minus(C--)的嵌入式脚本语言(最初绑定在CEnvi软件中),后将其改名Scr ...

  9. IDEA详细配置+优秀插件

    目录 IDEA破解 Settings配置 配置 settings 字体 关闭IDEA更新 设置IDEA打开为项目选择界面 自动导入包配置 显示方法的分割线 滚轮设置字体大小 智能提示忽略大小写 Tab ...

  10. 暑假自学java第八天

    1.接口的概念(关键字interface  ) Java程序设计中的接口 ( interface)也是一种规范,用来组织应用程序中的类,并调节它们的相互关系.接口是由常量和抽象方法组成的特殊类,是对抽 ...