Qt實(shí)現(xiàn)一個(gè)簡(jiǎn)單的word文檔編輯器
1.先看效果圖
可以設(shè)置文字的屬性、文字顏色、字體類型。以下示例僅供參考,有的地方還是不完善。

2.需要用到的類
2.1字體選擇下拉框:QFontComboBox。
QFontComboBox是一個(gè)讓用戶選擇字體的組合框。組合框中填充了按字母順序排列的字體族名稱列表。
常用方法:
獲取當(dāng)前的字體
QFont currentFont() const
還有一個(gè)信號(hào),當(dāng)字體發(fā)生改變時(shí),發(fā)送信號(hào)。
void currentFontChanged(const QFont &font)
2.2顏色對(duì)話框:QColorDialog
常用方法:
獲取當(dāng)前選擇的顏色
QColor currentColor() const
2.3QTextCharFormat
QTextCharFormat類為QTextDocument中的字符提供格式化信息。換句話說(shuō),我們要設(shè)置鼠標(biāo)選中字體的屬性,就需要使用這個(gè)類。
本例子中使用的方法:
| void setFont(const QFont &font) | 設(shè)置字體 |
| void setFontItalic(bool italic) | 設(shè)置是否斜體 |
| void setFontStrikeOut(bool strikeOut) | 設(shè)置刪除線 |
| void setFontUnderline(bool underline) | 設(shè)置下劃線 |
3.源碼
為了方便,我定義了5個(gè)全局變量
bool isBold = false; //是否粗體 bool isUnderLine = false; //是否下劃線 bool isDelLine = false; //是否刪除線 bool isLean = false; //是否斜體 QColor color(Qt::black); //字體顏色
設(shè)置斜體、粗體等按鈕可選中,因?yàn)槟J(rèn)是不可選中的,我們需要綁定可選中的信號(hào)。
ui->btnBold->setCheckable(true);
ui->btnDelLine->setCheckable(true);
ui->btnLean->setCheckable(true);
ui->btnUnderline->setCheckable(true);綁定按鈕的信號(hào)
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); //設(shè)置字體顏色
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();
}到此這篇關(guān)于Qt實(shí)現(xiàn)一個(gè)簡(jiǎn)單的word文檔編輯器的文章就介紹到這了,更多相關(guān)Qt word文檔編輯器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VS2019調(diào)試C語(yǔ)言程序(監(jiān)視操作)的詳細(xì)步驟
在很多時(shí)候我們?cè)趯?xiě)程序的過(guò)程中會(huì)發(fā)現(xiàn)一些非編程錯(cuò)誤的問(wèn)題,這樣的問(wèn)題很難直接分辨出來(lái),但是我們可以用調(diào)試了一步一步的模擬程序運(yùn)行的過(guò)程,來(lái)找出程序的錯(cuò)誤,下面這篇文章主要給大家介紹了關(guān)于VS2019調(diào)試C語(yǔ)言程序(監(jiān)視操作)的詳細(xì)步驟,需要的朋友可以參考下2022-11-11
C++實(shí)現(xiàn)掃雷小游戲(控制臺(tái)版)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)控制臺(tái)版的掃雷小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03
C++如何調(diào)用簡(jiǎn)單的python程序
這篇文章主要介紹了C++如何調(diào)用簡(jiǎn)單的python程序問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
利用Matlab實(shí)現(xiàn)迭代適應(yīng)點(diǎn)算法
道格拉斯-普克算法(Douglas–Peucker?algorithm,亦稱為拉默-道格拉斯-普克算法、迭代適應(yīng)點(diǎn)算法、分裂與合并算法)是將曲線近似表示為一系列點(diǎn),并減少點(diǎn)的數(shù)量的一種算法。本文將利用Matlab實(shí)現(xiàn)這一算法,需要的可以參考一下2022-04-04
教你用Matlab制作立體動(dòng)態(tài)相冊(cè)
沒(méi)想到吧,MATLAB竟也能制作3D相冊(cè)!本文將為大家詳細(xì)介紹Matlab制作立體動(dòng)態(tài)相冊(cè)的方法步驟,感興趣的小伙伴可以跟隨小編一起動(dòng)手試一試2022-03-03

