目录
一、搭建框架
在编写包含格式设置的文本编辑程序时,经常用到的 Qt 类有QTextEdit、QTextDocument、QTextBlock、QTextList、QTextFrame、QTextTable、QTextCharFormat、QTextBlockFormat、QTextListFormat、QTextFrameFormat 和 QTextTableFormat 等。
任何一个文本编辑的程序都要用到 QTextEdit 作为输入文本的容器,在它里面输入可编辑文本由 QTextDocument 作为载体,而用来表示 QTextDocument 的元素的QTextBlock、QTextList、QTextFrame 等是 QTextDocument 的不同表现形式,可以表示为字符串、段落、列表、表格或图片等。
每种元素都有自己的格式,这些格式则用 QTextCharFormat、QTextListFormat、QTextBlockFormat、QTextFrameFormat 等类来描述与实现。例如,QTextBlockFormat 类对应于 QTextBlock 类,QTextBlock 类用于表示一块文本,通常可以理解为一个段落,但它并不仅指段落;QTextBlockFormat 类则表示这一块文本的格式,如缩进的值、与四边的边距等。
从上图中可以看出用于表示编辑文本中的光标QTextCursor类是一个非常重要也经常会用到的类,它提供了对 QTextDocument 文档的修改接口,所有对文档格式的修改,说到底都与光标有关。例如,改变字符的格式,实际上指的是改变光标处字符的格式。又例如,改变段落的格式,实际上指的是改变光标所在段落的格式。因此,所有对 QTextDocument 的修改都能够通过 QTextCursor 类实现,QTextCursor 类在文档编辑类程序中有着重要的作用。
实现文本编辑功能的代码框架如下:
(1)在imgprocessor.h中添加private变量
QLabel *fontLabel1; //字体设置项
QFontComboBox *fontComboBox;
QLabel *fontLabel2;
QComboBox *sizeComboBox;
QToolButton *boldBtn;
QToolButton *italicBtn;
QToolButton *underlineBtn;
QToolButton *colorBtn;
QToolBar *fontToolBar; //字体工具栏
(2)在imgprocessor.h中添加protected slots变量
void ShowFontComboBox(QString comboStr);
void ShowSizeSpinBox(QString spinValue);
void ShowBoldBtn();
void ShowItalicBtn();
void ShowUnderlineBtn();
void ShowColorBtn();
void ShowCurrentFormatChanged(const QTextCharFormat &fmt);
(3)在相对应的构造函数中,在语句“setCentralWidget(showWidget); 与语句"createActions();"之间添加如下代码:
//在工具栏上嵌入控件
//设置字体
fontLabel1 =new QLabel(tr("字体:"));
fontComboBox =new QFontComboBox;
//设置为QFontComboBox::ScalableFonts,QFontComboBox 将只显示可缩放字体
//可缩放字体是指可以通过缩放或拉伸来调整大小的字体
//默认情况下为QFontComboBox::AllFonts 列出所有字体
fontComboBox->setFontFilters(QFontComboBox::ScalableFonts);
fontLabel2 =new QLabel(tr("字号:"));
sizeComboBox =new QComboBox;
//QFontDatabase是Qt中的一个类,它提供了一个接口来检索系统中可用的字体信息
QFontDatabase db;
//返回标准字体大小的列表
foreach(int size,db.standardSizes())
sizeComboBox->addItem(QString::number(size));
//创建按钮
boldBtn =new QToolButton;
boldBtn->setIcon(QIcon("bold.png"));
boldBtn->setCheckable(true);
italicBtn =new QToolButton;
italicBtn->setIcon(QIcon("italic.png"));
italicBtn->setCheckable(true);
underlineBtn =new QToolButton;
underlineBtn->setIcon(QIcon("underline.png"));
underlineBtn->setCheckable(true);
colorBtn =new QToolButton;
colorBtn->setIcon(QIcon("color.png"));
colorBtn->setCheckable(true);
(4) 在该构造函数的最后部分添加相关的事件关联:
connect(fontComboBox,SIGNAL(activated(QString)),this,SLOT(ShowFontComboBox(QString)));
connect(sizeComboBox,SIGNAL(activated(QString)),this,SLOT(ShowSizeSpinBox(QString)));
connect(boldBtn,SIGNAL(clicked()),this,SLOT(ShowBoldBtn()));
connect(italicBtn,SIGNAL(clicked()),this,SLOT(ShowItalicBtn()));
connect(underlineBtn,SIGNAL(clicked()),this,SLOT(ShowUnderlineBtn()));
connect(colorBtn,SIGNAL(clicked()),this,SLOT(ShowColorBtn()));
connect(showWidget->text,SIGNAL(currentCharFormatChanged(QtextCharFormat&)),this,SLOT(ShowCurrentFormatChanged(QTextCharFormat&)));
(5) 在相对应的工具栏 createToolBars()函数中添加如下代码:
//字体工具条
fontToolBar =addToolBar("Font");
fontToolBar->addWidget(fontLabel1);
fontToolBar->addWidget(fontComboBox);
fontToolBar->addWidget(fontLabel2);
fontToolBar->addWidget(sizeComboBox);
fontToolBar->addSeparator(); //添加一个分隔符
fontToolBar->addWidget(boldBtn);
fontToolBar->addWidget(italicBtn);
fontToolBar->addWidget(underlineBtn);
fontToolBar->addSeparator(); //添加一个分隔符
fontToolBar->addWidget(colorBtn);
二、设置字体
完成设置选定文字字体的函数 ShowFontComboBoxo代码如下:
void ImgProcessor::ShowFontComboBox(QString comboStr) //设置字体
{
QTextCharFormat fmt;
fmt.setFontFamily(comboStr); //选择的字体名称设置给 QTextCharFormat 对象
mergeFormat(fmt); //把新的格式应用到光标选区内的字符
}
所有对于 QTextDocument 进行的修改都通过 QTextCursor 类来完成,具体代码如下:
void ImgProcessor::mergeFormat(QTextCharFormat format)
{
//QTextDocument 进行的修改都通过 QTextCursor 类来完成
QTextCursor cursor =showWidget->text->textCursor(); //获得编辑框中的光标
if(!cursor.hasSelection())
cursor.select(QTextCursor::WordUnderCursor); //若光标没有高亮选区则将光标所在处的词作为选区,由前后空格或 等标","、"."点符号区分词
cursor.mergeCharFormat(format); //针对光标 //调用 QTextCursor fitl mergeCharFormat()函数将参数 format 所表示的格式应用到光标所在处的字符上
showWidget->text->mergeCurrentCharFormat(format);//针对字符 //调用 QTextEdit 的 mergeCurrentCharFormat()函数将格式应用到选区内的所有字符上
}
解释:
showWidget->text->mergeCurrentCharFormat(format);
这行代码的目的是将指定的格式 format
应用到文本部件 (showWidget
) 中当前字符的格式上。用于确保文本编辑器中应用的格式与之前设置的 format
保持一致,无论光标当前位置是在选中文本上还是在某个单词上。这样可以保证用户在编辑文本时,新输入的内容能够与已有的文本格式保持统一风格。
三、设置字号
设置选定文字字号大小的 ShowSizeSpinBox()函数代码如下:
void ImgProcessor::ShowSizeSpinBox(QString spinValue) //设置字号
{
QTextCharFormat fmt;
fmt.setFontPointSize(spinValue.toFloat());
showWidget->text->mergeCurrentCharFormat(fmt);
}
四、设置文字加粗
设置选定文字为加粗显示的ShowBoldBtn()函数代码如下:
void ImgProcessor::ShowBoldBtn() //设置文字显示加粗
{
QTextCharFormat fmt;
fmt.setFontWeight(boldBtn->isChecked()?QFont::Bold:QFont::Normal);
showWidget->text->mergeCurrentCharFormat(fmt);
}
五、设置文字倾斜
设置选定文字为斜体显示的 ShowItalicBtn()函数代码如下:
void ImgProcessor::ShowItalicBtn() //设置文字显示斜体
{
QTextCharFormat fmt;
fmt.setFontItalic(italicBtn->isChecked());
showWidget->text->mergeCurrentCharFormat(fmt);
}
六、文字加下划线
在选定文字下方加下画线的 ShowUnderlineBtn()函数代码如下:
void ImgProcessor::ShowUnderlineBtn() //设置文字加下画线
{
QTextCharFormat fmt;
fmt.setFontUnderline(underlineBtn->isChecked());
showWidget->text->mergeCurrentCharFormat(fmt);
}
七、设置文字颜色
设置选定文字颜色的 ShowColorBtn()函数代码如下:
void ImgProcessor::ShowColorBtn() //设置选定文字颜色
{
QColor color=QColorDialog::getColor(Qt::red,this);
if(color.isValid())
{
QTextCharFormat fmt;
fmt.setForeground(color);
showWidget->text->mergeCurrentCharFormat(fmt);
}
}
解释:
QColorDialog::getColor()函数原型:
QColor getColor
(
const QColor& initial=Qt::white, //指定了选中的颜色,默认为白色。通过 QColor::isValid()可以判断用户选择的颜色是否有效,若用户单击“取消“(Cancel) 按钮,
QColor::isValid()将返回false;
QWidget* parent=0 //指定父类
);
八、设置字符格式
当光标所在处的字符格式发生变化时调用此槽函数,函数根据新的字符格式将工具栏上各个格式控件的显示更新。
//当光标所在处的字符格式发生变化时调用此槽函数,函数根据新的字符格式将工具栏上各个格式控件的显示更新
void ImgProcessor::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());
}