Qt5开发学习之文本编辑功能(七)
来源:互联网 发布:阿沁淘宝店 编辑:程序博客网 时间:2024/06/02 02:01
Qt5文本编辑功能
在编写包含格式设置的文本编辑程序时,Qt用到的类一般为:QTextEdit,QTextDocument,QTextBlock,QTextFrame
等等。任何一个文本编辑器都要用到QTextEdit
作为文本的容器,在它里面输入的文本可由QTextDocument
作为载体,而用来表示QTextDocument
的元素的QTextBlock、QTextList、QTextFrame
等是QTextDocument
的不同表示方式,可以用来表示字符串、段落、列表、表格和图片等。
每种元素都有自己的格式,这些格式用QTextCharFormat、QTextBlockFormat、QTextListFormat、QTextFrameFormat
等类来描述和变现。例如:QTextBlock
类用于表示一块文本,通常用于理解为一个段落,QTextBlockFormat
类则表示这一块文本的格式,如缩进的值、与四边的边距等。
光标类QTextCursor
类是一个经常会用到的、非常重要的类。它提供了对QTextDocument
文档的修改接口,所有对文档格式的修改都和光标有关。改变的都是光标所在位置、行、段落的格式。
代码实例:完成一个文本编辑框,有改变字体、字号、加粗等功能:
首先创建一个QMainWindow
工程,在工程文件中新建一个QWidget
类,在QWidget
类中新建一个QTextEdit
对象,在QMainWindow
中加入QWidget
对象。
#include <QMainWindow>#include <QTextEdit>#include <QLabel>#include <QComboBox>#include <QFontComboBox>#include <QToolButton>#include <QToolBar>#include <QString>#include <QTextCharFormat>#include <QColorDialog>#include <QColor>#include "text.h"class TextEdit : public QMainWindow{ Q_OBJECTpublic: TextEdit(QWidget *parent = 0); ~TextEdit(); void meregeFormat(QTextCharFormat fmt);private: text *m_text; QLabel *fontLabel1; QFontComboBox *fontComboBox; QLabel *fontLabel2; QComboBox *sizeComboBox; QToolButton *boldBtn; QToolButton *italicBtn; QToolButton *underlineBtn; QToolButton *colorBtn; QToolBar *fontToolBar;protected slots: void showFontComboBox(QString comboStr); void showSizeSpinBox(QString spinValue); void showBoldBtn(); void showItalicBtn(); void showUnderLineBtn(); void showColorBtn(); void showCurrentFormatChanged(const QTextCharFormat &fmt);};
#include "textedit.h"TextEdit::TextEdit(QWidget *parent) : QMainWindow(parent){ this->resize(1000, 800); // 在主窗口添加输入区域 m_text = new text(this); setCentralWidget(m_text); fontLabel1 = new QLabel(tr("字体")); fontComboBox = new QFontComboBox; // 设置字体下拉框中可获取的字体 //fontComboBox->setFontFilters(QFontComboBox::ScalableFonts); fontLabel2 = new QLabel(tr("字号")); sizeComboBox = new QComboBox; // QFontDatabase 实现字号下拉列表框中显示字号 // standardSizes 函数返回标准字号的列表 QFontDatabase db; foreach (int size, db.standardSizes()) sizeComboBox->addItem(QString::number(size)); boldBtn = new QToolButton; boldBtn->setCheckable(true); italicBtn = new QToolButton; italicBtn->setCheckable(true); underlineBtn = new QToolButton; underlineBtn->setCheckable(true); colorBtn = new QToolButton; colorBtn->setCheckable(true); fontToolBar = addToolBar(tr("Font")); fontToolBar->addWidget(fontLabel1); fontToolBar->addWidget(fontComboBox); fontToolBar->addSeparator(); fontToolBar->addWidget(fontLabel2); fontToolBar->addWidget(sizeComboBox); fontToolBar->addSeparator(); fontToolBar->addWidget(boldBtn); fontToolBar->addWidget(italicBtn); fontToolBar->addWidget(underlineBtn); fontToolBar->addWidget(colorBtn); //connect(fontComboBox, &QFontComboBox::activated, this, &TextEdit::showFontComboBox); connect(fontComboBox, SIGNAL(activated(QString)), this, SLOT(showFontComboBox(QString))); //connect(sizeComboBox, &QComboBox::activated, this, &TextEdit::showSizeSpinBox); connect(sizeComboBox, SIGNAL(activated(QString)), this, SLOT(showSizeSpinBox(QString))); connect(boldBtn, &QToolButton::clicked, this, &TextEdit::showBoldBtn); connect(italicBtn, &QToolButton::clicked, this, &TextEdit::showItalicBtn); connect(underlineBtn, &QToolButton::clicked, this, &TextEdit::showUnderLineBtn); connect(colorBtn, &QToolButton::clicked, this, &TextEdit::showColorBtn); connect(m_text->edit, &QTextEdit::currentCharFormatChanged, this, &TextEdit::showCurrentFormatChanged);}TextEdit::~TextEdit(){}void TextEdit::meregeFormat(QTextCharFormat fmt){ // 获得编辑框的光标 QTextCursor cursor = m_text->edit->textCursor(); // 若没有高亮区域则光标所在处作为选区 if (!cursor.hasSelection()) { cursor.select(QTextCursor::WordUnderCursor); } cursor.mergeCharFormat(fmt); // 将所选用的字体作用于选择的区域 m_text->edit->mergeCurrentCharFormat(fmt);}void TextEdit::showFontComboBox(QString comboStr){ // 创建一个QTextCharFormat对象 QTextCharFormat fmt; // 选择的字体名称设置给QTextCharFormat对象 fmt.setFontFamily(comboStr); // 将新的格式应用到光标处 meregeFormat(fmt);}void TextEdit::showSizeSpinBox(QString spinValue){ QTextCharFormat fmt; fmt.setFontPointSize(spinValue.toFloat()); m_text->edit->mergeCurrentCharFormat(fmt);}void TextEdit::showBoldBtn(){ QTextCharFormat fmt; // setFontWidget 函数设置字体粗细, fmt.setFontWeight(boldBtn->isChecked() ? QFont::Bold : QFont::Normal); m_text->edit->mergeCurrentCharFormat(fmt);}void TextEdit::showItalicBtn(){ QTextCharFormat fmt; fmt.setFontItalic(italicBtn->isChecked()); m_text->edit->mergeCurrentCharFormat(fmt);}void TextEdit::showUnderLineBtn(){ QTextCharFormat fmt; fmt.setFontUnderline(underlineBtn->isChecked()); m_text->edit->mergeCurrentCharFormat(fmt);}void TextEdit::showColorBtn(){ QColor color = QColorDialog::getColor(Qt::red, this); if (color.isValid()) { QTextCharFormat fmt; fmt.setForeground(color); m_text->edit->mergeCurrentCharFormat(fmt); }}void TextEdit::showCurrentFormatChanged(const QTextCharFormat &fmt){ // 当光标所在处字符格式有变化,工具栏做出相应改变 fontComboBox->setCurrentIndex(fontComboBox->findText(fmt.fontFamily())); sizeComboBox->setCurrentIndex(sizeComboBox->findText(QString::number(fmt.fontPointSize()))); boldBtn->setChecked(fmt.font().bold()); italicBtn->setChecked(fmt.fontItalic()); underlineBtn->setChecked(fmt.fontUnderline());}
阅读全文
0 0
- Qt5开发学习之文本编辑功能(七)
- Qt5基础(六)实现Qt文本编辑功能学习笔记
- Qt5开发学习之事件(十三)
- Linux shell编程学习笔记(七) 文本编辑
- Qt5开发学习之主窗体(六)
- Qt5开发学习之Qt概述(一)
- Qt5开发学习之布局管理(三)
- Qt5开发学习之Qt基本对话框(四)
- Qt5开发学习之图形与SVG图像(八)
- Qt5开发学习之图形视图框架(九)
- Qt5开发学习之模型/视图结构(十)
- Qt5开发学习之文件及磁盘操作(十一)
- Qt5开发学习之网络与通信(十二)
- IOS 开发学习七 二维码扫描功能(ZBar SDK)
- Qt学习之路之文本编辑
- ArcGIS Engine 开发(二)线、圆、矩形、面、文本编辑功能
- Qt之文本编辑(一)
- Android开发之EditText文本编辑框…
- [js]01semantic&vue&reqwest实现数据的展示
- Mysql 基础知识
- PHP编程实现阳历转换为阴历的方法实例
- JavaScript进阶:JS的全局变量和全局函数
- 定时任务quartz
- Qt5开发学习之文本编辑功能(七)
- 相对路径、绝对路径、mysql及jdbc基础
- [Leetcode-146] LRU Cache 最近最少使用页面置换算法
- struct ip 和struct iphdr的差别 , <netinet/*.h>和<linux/*.h>
- POJ 2769 Reduced ID Numbers
- Accounting.js库的使用
- Docker 删除image
- 【Linux 学习】 文件系统
- 如何防止ISE综合时信号不被优化掉