Qt学习笔记第61到70讲

第61讲 记事本实现当前行高亮功能

实现策略: 获取当前行的光标位置,使用的信号和获取行列值是一样的,即通过ExtraSelection 来配置相关属性。
关键API:

cpp 复制代码
QList<QTextEdit::ExtraSelection> extraSelections;
void setExtraSelections(const QList<QTextEdit::ExtraSelection> &extraSelections)

ExtraSelection类内部有两个公有成员变量:

其中cursor可以存储获取的光标当前位置信息(比如行数),format用来存储文本样式属性。

综上所述,当前行高亮这个功能的实现还是需要在光标点击的响应槽函数onCursorPositionChanged内部。

第一步,声明了一个 QList 容器 extraSelections,用于存储 QTextEdit::ExtraSelection 类型的元素,并且明了一个 QTextEdit::ExtraSelection(结构体) 类型的变量 ext;

cpp 复制代码
QList <QTextEdit::ExtraSelection> extraSelections;
QTextEdit::ExtraSelection ext;

第二步,利用textEdit内部的textCursor方法获取当前行信息;

cpp 复制代码
 ext.cursor=ui->textEdit->textCursor();

第三步,开始配置颜色,创建了一个 QBrush 对象 qBrush,并使用 Qt::lightGray 颜色初始化它,然后通过 ext.format.setBackground(qBrush) 这行代码,将创建好的画刷所代表的颜色(浅灰色)设置为 ext 这个 ExtraSelection 实例对应的文本格式(ext.format)的背景颜色;

cpp 复制代码
QBrush qBrush(Qt::lightGray);
ext.format.setBackground(qBrush);

第四步,利用setProperty方法配置段属性为整行显示

cpp 复制代码
 ext.format.setProperty(QTextFormat::FullWidthSelection,true);

这行代码调用了 ext.formatQTextCharFormat 类型,用于设置文本的字符格式相关属性)的 setProperty 方法,将 QTextFormat::FullWidthSelection 属性设置为 trueQTextFormat::FullWidthSelection 这个属性表示文本选择区域按照整行的宽度来显示,确保不仅仅是当前光标所在处的文本有背景颜色等样式设置,而是整行文本都会应用之前配置的背景颜色(也就是浅灰色),实现整行高亮的效果。

第五步,进行整体配置

cpp 复制代码
extraSelections.append(ext);
ui->textEdit->setExtraSelections(extraSelections);

第一行 extraSelections.append(ext) 将配置好的 ext(代表当前行的额外选择区域相关属性)添加到 extraSelections 列表中。最后一行 ui->textEdit->setExtraSelections(extraSelections) 则调用了 textEdit 部件的 setExtraSelections 方法,将包含了当前行额外选择区域配置信息的 extraSelections 列表传递进去,使得 textEdit 部件按照这个列表里定义的规则(这里就是当前行高亮的相关属性设置)来更新其显示效果,最终实现在文本编辑部件中当前行被高亮显示的功能。

最后整个槽函数:

cpp 复制代码
void Widget::onCursorPositionChanged(void)
{
    QTextCursor cursor=ui->textEdit->textCursor();
    //qDebug()<<cursor.blockNumber()+1 <<cursor.columnNumber()+1;

    QString blockNum=QString::number(cursor.blockNumber()+1);
    QString columnNum=QString::number(cursor.columnNumber()+1);
    const QString labelMes="L:"+blockNum+" "+"C:"+columnNum;

    ui->labelPositon->setText(labelMes);

    /*设置当前行高亮*/
    //0.声明了一个 QList 容器 extraSelections
    QList <QTextEdit::ExtraSelection> extraSelections;
    //同时声明了ext这个结构体变量
    QTextEdit::ExtraSelection ext;

    //1.获取当前行的数值
    ext.cursor=ui->textEdit->textCursor();
    //2.配置颜色
    QBrush qBrush(Qt::lightGray);
    ext.format.setBackground(qBrush);
    //3.配置段属性,整行显示
    ext.format.setProperty(QTextFormat::FullWidthSelection,true);
    //4.整体配置
    extraSelections.append(ext);
    ui->textEdit->setExtraSelections(extraSelections);

}

运行效果:

相关推荐
碣石潇湘无限路26 分钟前
【AI篇】当Transformer模型开始学习《孙子兵法》
人工智能·学习
kikikidult2 小时前
(2025.07)解决——ubuntu20.04系统开机黑屏,左上角光标闪烁
笔记·ubuntu
future14122 小时前
C#每日学习日记
java·学习·c#
近津薪荼2 小时前
初学者关于数据在内存中的储存的笔记
笔记
碎叶城李白4 小时前
若依学习笔记1-validated
java·笔记·学习·validated
im_AMBER4 小时前
学习日志05 python
python·学习
真的想上岸啊5 小时前
学习C++、QT---18(C++ 记事本项目的stylesheet)
开发语言·c++·学习
HuashuiMu花水木6 小时前
PyTorch笔记1----------Tensor(张量):基本概念、创建、属性、算数运算
人工智能·pytorch·笔记
rui锐rui6 小时前
大数据学习2:HIve
大数据·hive·学习
凛铄linshuo7 小时前
爬虫简单实操2——以贴吧为例爬取“某吧”前10页的网页代码
爬虫·python·学习