当前位置:网站首页>Qt 一个简单的word文档编辑器
Qt 一个简单的word文档编辑器
2022-07-05 23:34:00 【Mr.codeee】
1.先看效果图
可以设置文字的属性、文字颜色、字体类型。以下示例仅供参考,有的地方还是不完善。

2.需要用到的类
QFontComboBox是一个让用户选择字体的组合框。组合框中填充了按字母顺序排列的字体族名称列表。
常用方法:
获取当前的字体
QFont currentFont() const
还有一个信号,当字体发生改变时,发送信号。
void currentFontChanged(const QFont &font)
2.2颜色对话框:QColorDialog
常用方法:
获取当前选择的颜色
QColor currentColor() const
2.3QTextCharFormat
QTextCharFormat类为QTextDocument中的字符提供格式化信息。换句话说,我们要设置鼠标选中字体的属性,就需要使用这个类。
本例子中使用的方法:
| void setFont(const QFont &font) | 设置字体 |
| void setFontItalic(bool italic) | 设置是否斜体 |
| void setFontStrikeOut(bool strikeOut) | 设置删除线 |
| void setFontUnderline(bool underline) | 设置下划线 |
3.源码
为了方便,我定义了5个全局变量
bool isBold = false; //是否粗体
bool isUnderLine = false; //是否下划线
bool isDelLine = false; //是否删除线
bool isLean = false; //是否斜体
QColor color(Qt::black); //字体颜色设置斜体、粗体等按钮可选中,因为默认是不可选中的,我们需要绑定可选中的信号。
ui->btnBold->setCheckable(true);
ui->btnDelLine->setCheckable(true);
ui->btnLean->setCheckable(true);
ui->btnUnderline->setCheckable(true);绑定按钮的信号
void clicked(bool checked = false)
#include "WTextEdit.h"
#include "ui_WTextEdit.h"
#include <QColorDialog>
#include <QTextDocument>
#include <QTextCursor>
#include <QTextCharFormat>
#include <QFont>
#include <QBrush>
bool isBold = false; //是否粗体
bool isUnderLine = false; //是否下划线
bool isDelLine = false; //是否删除线
bool isLean = false; //是否斜体
QColor color(Qt::black); //字体颜色
WTextEdit::WTextEdit(QWidget *parent) :
QWidget(parent),
ui(new Ui::WTextEdit)
{
ui->setupUi(this);
ui->btnBold->setCheckable(true);
ui->btnDelLine->setCheckable(true);
ui->btnLean->setCheckable(true);
ui->btnUnderline->setCheckable(true);
}
WTextEdit::~WTextEdit()
{
delete ui;
}
void WTextEdit::on_btnBold_clicked(bool checked)
{
isBold = checked;
updateText();
}
void WTextEdit::on_btnLean_clicked(bool checked)
{
isLean = checked;
updateText();
}
void WTextEdit::on_btnUnderline_clicked(bool checked)
{
isUnderLine = checked;
updateText();
}
void WTextEdit::on_btnDelLine_clicked(bool checked)
{
isDelLine = checked;
updateText();
}
void WTextEdit::updateText()
{
QFont font = ui->fontComboBox->currentFont();
font.setBold(isBold);
font.setPointSize(ui->lineEdit->text().toInt());
QTextCharFormat format;
format.setFont(font);
format.setFontItalic(isLean);
format.setFontStrikeOut(isDelLine);
format.setFontUnderline(isUnderLine);
QPen pen;
pen.setColor(color); //设置字体颜色
format.setTextOutline(pen);
ui->textEdit->textCursor().setCharFormat(format);
}
void WTextEdit::on_btnColor_clicked()
{
QColorDialog dialog;
dialog.exec();
color = dialog.currentColor();
updateText();
}
void WTextEdit::on_lineEdit_textChanged(const QString &arg1)
{
updateText();
}
void WTextEdit::on_fontComboBox_currentFontChanged(const QFont &f)
{
updateText();
}
边栏推荐
- Objective C message dispatch mechanism
- [original] what is the core of programmer team management?
- VS2010 writes DLL and unit test of dynamic link library, and transfers the correctness of DLL test
- 21. PWM application programming
- It is proved that POJ 1014 module is optimized and pruned, and some recursion is wrong
- 2022.6.20-6.26 AI行业周刊(第103期):新的小生命
- Zero rhino technology joined hands with the intelligence Club: the "causal faction" forum was successfully held, and the "causal revolution" brought the next generation of trusted AI
- 带外和带内的区别
- 同事悄悄告诉我,飞书通知还能这样玩
- idea 连接mysql ,直接贴配置文件的url 比较方便
猜你喜欢

GFS分布式文件系统

Zero rhino technology joined hands with the intelligence Club: the "causal faction" forum was successfully held, and the "causal revolution" brought the next generation of trusted AI

开源crm客户关系统管理系统源码,免费分享

The use of El cascader and the solution of error reporting

零犀科技携手集智俱乐部:“因果派”论坛成功举办,“因果革命”带来下一代可信AI

Redis高可用——主从复制、哨兵模式、集群

激光slam学习记录

Spreadjs 15.1 CN and spreadjs 15.1 en

开关电源Buck电路CCM及DCM工作模式

Neural structured learning 4 antagonistic learning for image classification
随机推荐
零犀科技携手集智俱乐部:“因果派”论坛成功举办,“因果革命”带来下一代可信AI
Breadth first search open turntable lock
698. Divided into k equal subsets ●●
SpreadJS 15.1 CN 与 SpreadJS 15.1 EN
Latex multiple linebreaks
In C#, why can't I modify the member of a value type instance in a foreach loop?
asp. Net pop-up layer instance
Scala concurrent programming (II) akka
Switching power supply buck circuit CCM and DCM working mode
2022.6.20-6.26 AI行业周刊(第103期):新的小生命
MySQL (2) -- simple query, conditional query
Rasa 3.x 学习系列-Rasa 3.2.1 新版本发布
Naoqi robot summary 26
QCombox(重写)+QCompleter(自动补全,自动加载qcombox的下拉选项,设置背景颜色)
The interface of grafana tool displays an error, incluxdb error
Live tiktok shop 2022 latest gameplay card slot overseas live e-commerce new traffic
5. Logistic regression
98. 验证二叉搜索树 ●●
UVA11294-Wedding(2-SAT)
Which side projects can be achieved? Is it difficult for we media to earn more than 10000 a month?