当前位置:网站首页>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();
}
边栏推荐
- [Yu Yue education] NC machining technology reference materials of Shaanxi University of science and technology
- 跟着CTF-wiki学pwn——ret2libc1
- Initialize your vector & initializer with a list_ List introduction
- CAS and synchronized knowledge
- 妙才周刊 - 8
- 俄外交部:日韩参加北约峰会影响亚洲安全稳定
- PADS ROUTER 使用技巧小记
- Spécifications techniques et lignes directrices pour la sélection des tubes TVS et ESD - Recommandation de jialichuang
- Bao Yan notebook IV software engineering and calculation volume II (Chapter 8-12)
- The use of El cascader and the solution of error reporting
猜你喜欢
Spécifications techniques et lignes directrices pour la sélection des tubes TVS et ESD - Recommandation de jialichuang
GFS Distributed File System
CAS and synchronized knowledge
Hcip course notes-16 VLAN, three-tier architecture, MPLS virtual private line configuration
Senparc.Weixin.Sample.MP源码剖析
TVS管 与 稳压二极管参数对比
CIS基准测试工具kube-bench使用
[classical control theory] summary of automatic control experiment
如何让同步/刷新的图标(el-icon-refresh)旋转起来
Bao Yan notebook IV software engineering and calculation volume II (Chapter 8-12)
随机推荐
Zhongjun group launched electronic contracts to accelerate the digital development of real estate enterprises
Fiddler Everywhere 3.2.1 Crack
698. 划分为k个相等的子集 ●●
Biased sample variance, unbiased sample variance
TS type declaration
Rethinking about MySQL query optimization
JVM details
SpreadJS 15.1 CN 与 SpreadJS 15.1 EN
Bao Yan notebook IV software engineering and calculation volume II (Chapter 8-12)
VBA fast switching sheet
21.PWM应用编程
2022.6.20-6.26 AI行业周刊(第103期):新的小生命
Difference between out of band and in band
Spire.PDF for NET 8.7.2
STM32__ 06 - single channel ADC
Spécifications techniques et lignes directrices pour la sélection des tubes TVS et ESD - Recommandation de jialichuang
Live tiktok shop 2022 latest gameplay card slot overseas live e-commerce new traffic
Rasa 3. X learning series -rasa 3.2.1 new release
教你在HbuilderX上使用模拟器运行uni-app,良心教学!!!
PADS ROUTER 使用技巧小记