Qt富文本查找

1.在mainwindow.h 添加声明

cpp 复制代码
class QLineEdit;
class QDialog;

    void textFind();    //查找文本
    void textNext();    //查找下一个

2.在cpp里面进行添加、布局、定义

添加查找和布局窗口

cpp 复制代码
 //查找
    QAction *action_textfind = new QAction("查找",this);
    connect(action_textfind,&QAction::triggered,
            this,&MainWindow::textFind);
    ui->mainToolBar->addAction(action_textfind);

    m_findDialg = new QDialog(this); //查找对话框
    m_lineEdit = new QLineEdit(m_findDialg); //查找输入框
    QPushButton *btn = new QPushButton(m_findDialg);
    btn->setText("查找下一个");
    connect(btn,&QPushButton::clicked,
            this,&MainWindow::textNext);
    //进行垂直布局
    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(m_lineEdit);
    layout->addWidget(btn);
    m_findDialg->setLayout(layout);

对第一步的声明进行定义

cpp 复制代码
void MainWindow::textFind()
{
    m_findDialg->show();
}

void MainWindow::textNext()
{
    QString strFind = m_lineEdit->text();
    bool isFind = ui->textEdit->find(strFind,QTextDocument::FindBackward);
    if(isFind){
        qDebug()<<QString("行号:%1,列号:%2")
                  .arg(ui->textEdit->textCursor().blockNumber())
                  .arg(ui->textEdit->textCursor().columnNumber());
    }

}
相关推荐
Felix_One5 天前
Qt 串口通信避坑指南:QSerialPort 的 5 个常见问题
qt
blasit8 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
范特西.i13 天前
QT聊天项目(8)
开发语言·qt
枫叶丹413 天前
【Qt开发】Qt界面优化(七)-> Qt样式表(QSS) 样式属性
c语言·开发语言·c++·qt
十五年专注C++开发13 天前
Qt deleteLater作用及源码分析
开发语言·c++·qt·qobject
kangzerun13 天前
SQLiteManager:一个优雅的Qt SQLite数据库操作类
数据库·qt·sqlite
金刚狼8813 天前
qt和qt creator的下载安装
开发语言·qt
追烽少年x13 天前
Qt中使用Zint库显示二维码
qt
谁刺我心13 天前
qt源码、qt在线安装器镜像下载
开发语言·qt
金刚狼8813 天前
在qt creator中创建helloworld程序并构建
开发语言·qt