当前位置:网站首页>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();
}
边栏推荐
- Rasa 3. X learning series -rasa x Community Edition (Free Edition) changes
- 5. Logistic regression
- JVM details
- 跟着CTF-wiki学pwn——ret2libc1
- CIS基准测试工具kube-bench使用
- Technical specifications and model selection guidelines for TVs tubes and ESD tubes - recommended by jialichuang
- C# 反射与Type
- MySQL delete uniqueness constraint unique
- [EF core] mapping relationship between EF core and C data type
- When to use useImperativeHandle, useLayoutEffect, and useDebugValue
猜你喜欢
激光slam学习记录
Spire Office 7.5.4 for NET
Xinyuan & Lichuang EDA training camp - brushless motor drive
CAS and synchronized knowledge
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?
Laser slam learning record
CIS benchmark tool Kube bench
Dynamic planning: robbing families and houses
Initialiser votre vecteur & initialisateur avec une liste Introduction à la Liste
Initialize your vector & initializer with a list_ List introduction
随机推荐
TS type declaration
shardingsphere源码解析
How to rotate the synchronized / refreshed icon (EL icon refresh)
成为程序员的你,后悔了吗?
CAS and synchronized knowledge
Xinyuan & Lichuang EDA training camp - brushless motor drive
QCombox(重写)+QCompleter(自动补全,自动加载qcombox的下拉选项,设置背景颜色)
如何提升口才
15 MySQL stored procedures and functions
Rasa 3. X learning series -rasa x Community Edition (Free Edition) changes
【LeetCode】5. Valid palindrome
98. 验证二叉搜索树 ●●
Cwaitabletimer timer, used to create timer object access
Qt 一个简单的word文档编辑器
How to get all the values stored in localstorage
如何获取localStorage中存储的所有值
Golang code checking tool
698. 划分为k个相等的子集 ●●
White hat talks about web security after reading 2
Make a short video clip number of we media film and television. Where can I download the material?