網(wǎng)站首頁 編程語言 正文
1.先看效果圖
可以設(shè)置文字的屬性、文字顏色、字體類型。以下示例僅供參考,有的地方還是不完善。
2.需要用到的類
2.1字體選擇下拉框:QFontComboBox。
QFontComboBox是一個讓用戶選擇字體的組合框。組合框中填充了按字母順序排列的字體族名稱列表。
常用方法:
獲取當(dāng)前的字體
QFont currentFont() const
還有一個信號,當(dāng)字體發(fā)生改變時,發(fā)送信號。
void currentFontChanged(const QFont &font)
2.2顏色對話框:QColorDialog
常用方法:
獲取當(dāng)前選擇的顏色
QColor currentColor() const
2.3QTextCharFormat
QTextCharFormat類為QTextDocument中的字符提供格式化信息。換句話說,我們要設(shè)置鼠標(biāo)選中字體的屬性,就需要使用這個類。
本例子中使用的方法:
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è)置斜體、粗體等按鈕可選中,因為默認(rèn)是不可選中的,我們需要綁定可選中的信號。
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();
}
原文鏈接:https://blog.csdn.net/wzz953200463/article/details/125608220
相關(guān)推薦
- 2022-10-07 Android?Gradle?三方依賴管理詳解_Android
- 2022-11-07 python學(xué)習(xí)pymongo模塊的使用方法_python
- 2022-03-22 C++實現(xiàn)轉(zhuǎn)置矩陣的循環(huán)(矩陣轉(zhuǎn)置函數(shù))
- 2022-09-03 C++中std::conditional的使用說明_C 語言
- 2022-10-23 Python?Pandas數(shù)據(jù)合并pd.merge用法詳解_python
- 2022-10-29 編寫字符設(shè)備驅(qū)動控制樹莓派io口
- 2023-02-17 python引入其他py文件或模塊_python
- 2022-11-04 Linux下自動刪除過期備份和自動異地備份的腳本_linux shell
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支