5.2 Qt5文本编辑功能
5.2 Qt5的文本编辑功能
在上一节我们初步了解到,对于一个QTextEdit,它其中包含有一个document()函数,该函数会返回一个QTextDocument的对象,那么实际上这个对象就是专门用于对纯文本文档来进行描述的一个类。实际上一个QTextDocument中包含了不同类型的文档元素(QTextTable、QTextList、QTextBlock、QTextFrame)以及描述这些文档元素格式的类型(QTextCharFormat、QTextTableFormat、QTextListFormat、QTextBlockFormat、QTextFrameFormat),除此之外,我们还会通过QTextCursor来描述文档中元素的位置,以便于可以对它们进行一些操作。具体的结构图如下:

然后一个文本编辑的程序都需要用到QTextEdit来作为输入文本的容器,而在这个QTextEdit里面又是以QTextDocument来作为载体的,而一个QTextDocument又是又多种元素构成的,例如QTextTable、QTextFrame、QTextBlock、QTextList。对于QTextBlock,可以理解为它是用来表示一块文本内容,可以把它理解为一个段落,但它不仅仅是一个段落那么简单。而QTextBlockFormat则表示这一段内容的文本的格式,比如缩进的值,与窗体四边的边距等。
另外可以看出,QTextCursor在整个结构中扮演了修改文档内容的重要作用。它提供了修改QTextDocument的修改接口,实际上所有对文档格式的修改,最终都被抽象为对光标所在字符或上下文的修改。例如修改段落的格式,实际上就是修改的光标所在段落的格式。因此对QTextDocument的修改都能通过QTextCursor来实现。
5.2.1 实现文本编辑的具体操作如下:
(1)在imgprocessor.h中添加如下成员:
public:
void createFontWidget();//创建和字体有关的Widget
private:
QLabel*m_fontLabel;//字体Label
QFontComboBox*m_fontComboBox;//字体ComoBox
QLabel*m_fontSizeLabel;//字体大小Label
QComboBox*m_fontSizeComoBox;//字体大小ComoBox
QToolButton*m_boldToolButton;//字体加粗按钮
QToolButton*m_italicToolButton;//字体斜体按钮
QToolButton*m_underLineToolButton;//字体下划线按钮
QToolButton*m_colorToolButton;//字体颜色按钮
QToolBar*m_fontToolBar;//字体工具栏
private slots:
void ShowFontComboBox(const QFont &f);//字体ComboBox处理
void ShowFontSizeComboBox(QString);//字体大小ComboBox处理
void ShowBoldToolButton();//加粗按钮
void ShowItalicToolButton();//斜体
void ShowUnderlineToolButton();//下划线
void ShowColorToolButton();//字体颜色
void ShowCurrrentFormatChanged(const QTextCharFormat &f);//字体格式改变
(2)在imgprocessor.cpp中进行实现
void ImgProcessor::createFontWidget()
{
m_fontLabel = new QLabel(tr("字体"));
m_fontComboBox = new QFontComboBox;
m_fontComboBox->setFontFilters(QFontComboBox::FontFilter::ScalableFonts);
connect(m_fontComboBox,&QFontComboBox::currentFontChanged,this,&ImgProcessor::ShowFontComboBox);
m_fontSizeLabel = new QLabel(tr("字号"));
m_fontSizeComoBox = new QComboBox;
QFontDatabase db;
for(size_t index = 0;index < db.standardSizes().size();index++)
{
m_fontSizeComoBox->addItem(QString("%1").arg(db.standardSizes().operator[](index)));
}
connect(m_fontSizeComoBox,SIGNAL(activated(QString)),this,SLOT(ShowFontSizeComboBox(QString)));
m_boldToolButton = new QToolButton;
m_boldToolButton->setIcon(QIcon(":/bold.png"));
m_boldToolButton->setStatusTip(tr("加粗"));
m_boldToolButton->setCheckable(true);
connect(m_boldToolButton,&QToolButton::clicked,this,&ImgProcessor::ShowBoldToolButton);
m_italicToolButton = new QToolButton;
m_italicToolButton->setIcon(QIcon(":/Italian.png"));
m_italicToolButton->setStatusTip(tr("斜体"));
m_italicToolButton->setCheckable(true);
connect(m_italicToolButton,&QToolButton::clicked,this,&ImgProcessor::ShowItalicToolButton);
m_underLineToolButton = new QToolButton;
m_underLineToolButton->setIcon(QIcon(":/U.png"));
m_underLineToolButton->setStatusTip(tr("下划线"));
m_underLineToolButton->setCheckable(true);
connect(m_underLineToolButton,&QToolButton::clicked,this,&ImgProcessor::ShowUnderlineToolButton);
m_colorToolButton = new QToolButton;
m_colorToolButton->setIcon(QIcon(":/side.png"));
m_colorToolButton->setStatusTip(tr("字体颜色"));
connect(m_colorToolButton,&QToolButton::clicked,this,&ImgProcessor::ShowColorToolButton);
connect(m_showWidget->m_text,&QTextEdit::currentCharFormatChanged,this,&ImgProcessor::ShowCurrrentFormatChanged);
}
//字体选择ComboBox
void ImgProcessor::ShowFontComboBox(const QFont &f)
{
qDebug() << f;
QTextCharFormat format;//创建QTextCharFormat对象
format.setFontFamily(f.family());//对字符格式设置字体族
mergeFormat(format);//将设置的字符应用到光标的所选的字符上
}
//字体大小选择ComboBox
void ImgProcessor::ShowFontSizeComboBox(QString fontSize)
{
QTextCharFormat format = m_showWidget->m_text->currentCharFormat();//获取默认的charFormat
format.setFontPointSize(fontSize.toUInt());//设置其字体格式大小
m_showWidget->m_text->mergeCurrentCharFormat(format);//将修改后的字体合并到默认的CharFormat中
}
//字体加粗
void ImgProcessor::ShowBoldToolButton()
{
QTextCursor cursor = m_showWidget->m_text->textCursor();
if(!cursor.hasSelection())
{
cursor.select(QTextCursor::SelectionType::BlockUnderCursor);
if(m_boldToolButton->isChecked())
{
QFont font = m_showWidget->m_text->currentFont();
font.setBold(true);
m_showWidget->m_text->setCurrentFont(font);
m_boldToolButton->setChecked(true);
}
else
{
QFont font = m_showWidget->m_text->currentFont();
font.setBold(false);
m_showWidget->m_text->setCurrentFont(font);
m_boldToolButton->setChecked(false);
}
}
else
{
if(m_boldToolButton->isChecked())
{
QFont font = m_showWidget->m_text->currentFont();
font.setBold(true);
m_showWidget->m_text->setCurrentFont(font);
m_boldToolButton->setChecked(true);
}
else
{
QFont font = m_showWidget->m_text->currentFont();
font.setBold(false);
m_showWidget->m_text->setCurrentFont(font);
m_boldToolButton->setChecked(false);
}
}
}
//斜体
void ImgProcessor::ShowItalicToolButton()
{
QTextCursor cursor = m_showWidget->m_text->textCursor();
if(!cursor.hasSelection())
{
cursor.select(QTextCursor::WordUnderCursor);
QTextCharFormat format = m_showWidget->m_text->currentCharFormat();
if(m_italicToolButton->isChecked())
{
m_italicToolButton->setChecked(true);
format.setFontItalic(true);
}
else
{
m_italicToolButton->setChecked(false);
format.setFontItalic(false);
}
m_showWidget->m_text->setCurrentCharFormat(format);
}
else
{
QTextCharFormat format = m_showWidget->m_text->currentCharFormat();
if(m_italicToolButton->isChecked())
{
m_italicToolButton->setChecked(true);
format.setFontItalic(true);
}
else
{
m_italicToolButton->setChecked(false);
format.setFontItalic(false);
}
m_showWidget->m_text->setCurrentCharFormat(format);
}
}
//字体下划线
void ImgProcessor::ShowUnderlineToolButton()
{
QFont font = m_showWidget->m_text->currentFont();
QTextCursor cursor = m_showWidget->m_text->textCursor();
if(!cursor.hasSelection())
{
cursor.select(QTextCursor::SelectionType::BlockUnderCursor);
if(m_underLineToolButton->isChecked())
{
font.setUnderline(true);
m_underLineToolButton->setChecked(true);
}
else
{
font.setUnderline(false);
m_underLineToolButton->setChecked(false);
}
m_showWidget->m_text->setCurrentFont(font);
}
else
{
if(m_underLineToolButton->isChecked())
{
font.setUnderline(true);
m_underLineToolButton->setChecked(true);
}
else
{
font.setUnderline(false);
m_underLineToolButton->setChecked(false);
}
m_showWidget->m_text->setCurrentFont(font);
}
}
//显示字体颜色面板
void ImgProcessor::ShowColorToolButton()
{
QColor color = QColorDialog::getColor(Qt::red,this);
QTextCharFormat format = m_showWidget->m_text->currentCharFormat();
format.setForeground(color);
m_showWidget->m_text->mergeCurrentCharFormat(format);
}
//文字格式改变
void ImgProcessor::ShowCurrrentFormatChanged(const QTextCharFormat &f)
{
m_fontComboBox->setCurrentIndex(m_fontComboBox->findText(f.fontFamily()));
m_fontSizeComoBox->setCurrentIndex(m_fontSizeComoBox->findText(QString::number(f.fontPointSize())));
m_boldToolButton->setChecked(f.font().bold());
m_italicToolButton->setChecked(f.fontItalic());
m_underLineToolButton->setChecked(f.font().underline());
}
最终效果图:

在下一节将介绍如何对文本内容进行排版的操作(包括左对齐,右对齐,居中对齐和两端对齐以及如何对某一段落进行对齐的操作)
本小节代码:https://files.cnblogs.com/files/blogs/792763/ImageProcessor5.2.zip?t=1716375769&download=true

浙公网安备 33010602011771号