当前位置:网站首页>QT a simple word document editor
QT a simple word document editor
2022-07-05 23:49:00 【Mr.codeee】
1. Look at the renderings first
You can set the properties of text 、 Text color 、 Font type . The following example is for reference only , Some places are still imperfect .
2. Classes that need to be used
2.1 Font selection drop-down box :QFontComboBox.
QFontComboBox It is a combo box that allows users to select fonts . The combo box is filled with an alphabetical list of font family names .
Common methods :
Get the current font
QFont currentFont() const
There's another signal , When the font changes , Sending signal .
void currentFontChanged(const QFont &font)
2.2 Color dialog :QColorDialog
Common methods :
Get the currently selected color
QColor currentColor() const
2.3QTextCharFormat
QTextCharFormat Class is QTextDocument The characters in provide formatting information . let me put it another way , We need to set the properties of the font selected by the mouse , You need to use this class .
The method used in this example :
void setFont(const QFont &font) | Set the font |
void setFontItalic(bool italic) | Set whether Italic |
void setFontStrikeOut(bool strikeOut) | Set strikethrough |
void setFontUnderline(bool underline) | Set underline |
3. Source code
For convenience , I defined 5 Global variables
bool isBold = false; // Is it bold
bool isUnderLine = false; // Whether to underline
bool isDelLine = false; // Delete line
bool isLean = false; // Is it italicized
QColor color(Qt::black); // The font color
Set italics 、 Buttons such as bold can be selected , Because the default is not selectable , We need to bind selectable signals .
ui->btnBold->setCheckable(true);
ui->btnDelLine->setCheckable(true);
ui->btnLean->setCheckable(true);
ui->btnUnderline->setCheckable(true);
Signal of binding button
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; // Is it bold
bool isUnderLine = false; // Whether to underline
bool isDelLine = false; // Delete line
bool isLean = false; // Is it italicized
QColor color(Qt::black); // The font color
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); // Set font 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();
}
边栏推荐
- 用列表初始化你的vector&&initializer_list简介
- What if the C disk is not enough? Let's see how I can clean up 25g of temp disk space after I haven't redone the system for 4 years?
- GFS distributed file system
- 11gR2 Database Services for &quot; Policy&quot; and &quot; Administrator&quot; Managed databases (file I
- Russian Foreign Ministry: Japan and South Korea's participation in the NATO summit affects security and stability in Asia
- CIS基准测试工具kube-bench使用
- QCombox(重写)+QCompleter(自动补全,自动加载qcombox的下拉选项,设置背景颜色)
- SpreadJS 15.1 CN 与 SpreadJS 15.1 EN
- Live tiktok shop 2022 latest gameplay card slot overseas live e-commerce new traffic
- TVS管 与 稳压二极管参数对比
猜你喜欢
Rasa 3. X learning series -rasa 3.2.1 new release
How to get all the values stored in localstorage
CAS and synchronized knowledge
Tips for using pads router
Initialiser votre vecteur & initialisateur avec une liste Introduction à la Liste
GFS分布式文件系統
GFS distributed file system
98. 验证二叉搜索树 ●●
Fiddler Everywhere 3.2.1 Crack
JVM details
随机推荐
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
Spreadjs 15.1 CN and spreadjs 15.1 en
Golang code checking tool
Qt 一个简单的word文档编辑器
用列表初始化你的vector&&initializer_list简介
Spire Office 7.5.4 for NET
JVM details
Neural structured learning - Part 3: training with synthesized graphs
俄外交部:日韩参加北约峰会影响亚洲安全稳定
如何获取localStorage中存储的所有值
Russian Foreign Ministry: Japan and South Korea's participation in the NATO summit affects security and stability in Asia
Use mapper: --- tkmapper
Différence entre hors bande et en bande
20.移植Freetype字体库
Go language introduction detailed tutorial (I): go language in the era
el-cascader的使用以及报错解决
18.(arcgis api for js篇)arcgis api for js点采集(SketchViewModel)
15 MySQL-存储过程与函数
多普勒效應(多普勒頻移)
Introduction to JVM