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());}