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

}

运行效果:

相关推荐
峥嵘life6 分钟前
Android16 【CTS】CtsWindowManagerDeviceAnimations存在fail项
android·linux·学习
宵时待雨26 分钟前
C++笔记归纳10:继承
开发语言·数据结构·c++·笔记·算法
Xudde.30 分钟前
班级作业笔记报告0x02
笔记
Hello_Embed33 分钟前
LVGL 入门(一):环境搭建与源码获取
笔记·stm32·单片机·嵌入式·lvgl
微露清风2 小时前
系统性学习Linux-第六讲-Ext文件系统
linux·服务器·学习
泯仲3 小时前
从零起步学习MySQL 第十章:深入了解B+树及B+树的性能优势
b树·学习·mysql
2501_926978333 小时前
AI在精神世界的意义--对存在模式的重新解构
经验分享·笔记·ai写作
做怪小疯子3 小时前
Python 基础学习
开发语言·python·学习
爱倒腾的老唐3 小时前
1、万用表
笔记
runafterhit4 小时前
AI基础学习-基础概念汇总
人工智能·学习