服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - Qt实现一个简单的word文档编辑器

Qt实现一个简单的word文档编辑器

2023-02-21 16:43Mr.codeee C/C++

本文主要介绍了Qt实现一个简单的word文档编辑器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

1.先看效果图

可以设置文字的属性、文字颜色、字体类型。以下示例仅供参考,有的地方还是不完善。

Qt实现一个简单的word文档编辑器

2.需要用到的类

2.1字体选择下拉框:QFontComboBox。

QFontComboBox是一个让用户选择字体的组合框。组合框中填充了按字母顺序排列的字体族名称列表。

常用方法:

获取当前的字体

?
1
QFont currentFont() const

还有一个信号,当字体发生改变时,发送信号。

?
1
void currentFontChanged(const QFont &font)

2.2颜色对话框:QColorDialog

常用方法:

获取当前选择的颜色

?
1
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个全局变量

?
1
2
3
4
5
6
bool isBold = false;    //是否粗体
bool isUnderLine = false; //是否下划线
bool isDelLine = false; //是否删除线
bool isLean = false; //是否斜体
 
QColor color(Qt::black); //字体颜色

设置斜体、粗体等按钮可选中,因为默认是不可选中的,我们需要绑定可选中的信号。

?
1
2
3
4
ui->btnBold->setCheckable(true);
ui->btnDelLine->setCheckable(true);
ui->btnLean->setCheckable(true);
ui->btnUnderline->setCheckable(true);

绑定按钮的信号

void clicked(bool checked = false)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#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();
}

到此这篇关于Qt实现一个简单的word文档编辑器的文章就介绍到这了,更多相关Qt word文档编辑器内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/wzz953200463/article/details/125608220

延伸 · 阅读

精彩推荐