最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Qt實現(xiàn)一個簡單的word文檔編輯器

 更新時間:2022年07月05日 11:24:09   作者:Mr.codeee  
本文主要介紹了Qt實現(xiàn)一個簡單的word文檔編輯器,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

1.先看效果圖

可以設(shè)置文字的屬性、文字顏色、字體類型。以下示例僅供參考,有的地方還是不完善。

2.需要用到的類

2.1字體選擇下拉框:QFontComboBox。

QFontComboBox是一個讓用戶選擇字體的組合框。組合框中填充了按字母順序排列的字體族名稱列表。

常用方法:

獲取當前的字體

QFont currentFont() const

還有一個信號,當字體發(fā)生改變時,發(fā)送信號。

void currentFontChanged(const QFont &font)

2.2顏色對話框:QColorDialog

常用方法:

獲取當前選擇的顏色

QColor currentColor() const

2.3QTextCharFormat

QTextCharFormat類為QTextDocument中的字符提供格式化信息。換句話說,我們要設(shè)置鼠標選中字體的屬性,就需要使用這個類。

本例子中使用的方法:

void setFont(const QFont &font)設(shè)置字體
void setFontItalic(bool italic)設(shè)置是否斜體
void setFontStrikeOut(bool strikeOut)設(shè)置刪除線
void setFontUnderline(bool underline)設(shè)置下劃線

3.源碼

為了方便,我定義了5個全局變量

bool isBold = false;    //是否粗體
bool isUnderLine = false; //是否下劃線
bool isDelLine = false; //是否刪除線
bool isLean = false; //是否斜體
 
QColor color(Qt::black); //字體顏色

設(shè)置斜體、粗體等按鈕可選中,因為默認是不可選中的,我們需要綁定可選中的信號。

    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);    //設(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實現(xiàn)一個簡單的word文檔編輯器的文章就介紹到這了,更多相關(guān)Qt word文檔編輯器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

谷城县| 阿克陶县| 刚察县| 天峨县| 阳新县| 保定市| 剑川县| 邓州市| 海晏县| 浑源县| 山东省| 扶风县| 读书| 华安县| 自治县| 鄂托克前旗| 利辛县| 宝兴县| 萨迦县| 山东| 吴堡县| 榆社县| 灌云县| 盐亭县| 岑巩县| 麦盖提县| 阿拉善右旗| 灵璧县| 浠水县| 繁昌县| 龙南县| 浦东新区| 云阳县| 电白县| 丹阳市| 东宁县| 元阳县| 甘洛县| 平和县| 盐津县| 肇州县|