200_QT_自行构建一个QDialog对话框,完成内容的点击搜索

65 篇文章 6 订阅
/*
查找文本信息
注意这里是功能按键,所发信号属于QAction类
*/
    connect(paction_find, &QAction::triggered, this, [=](){

        QDialog *dia = new QDialog(this);
        dia->setWindowTitle("查找文本信息");
        dia->resize(160,90);
        dia->setModal(true);

        //标签、文本框、按钮,都创建在对话框里面
        QLabel *label = new QLabel(tr("点击查找"),dia);
        label->move(22,5);
        textLineEdit = new QLineEdit(dia);
        textLineEdit->setFixedSize(60,38);
        textLineEdit->setStyleSheet ("border:2px groove gray;border-radius:10px;padding:2px 4px;"      "color:rgb(110,174,248)");
        textLineEdit->move(20,20);

        btn = new QPushButton(dia);
        btn->setFixedSize(60,38);
        btn->setStyleSheet ("border:2px groove gray;border-radius:10px;padding:2px 4px;"      "color:rgb(110,174,248)");
        btn->setText("搜索文本");
        btn->move(80,20);


        dia->show();

        connect(btn, &QPushButton::clicked, this, [=](){

            QString str = textLineEdit->text();
            qDebug() << "QString str = " << str << endl;

            bool ok = ptextedit->find(str, QTextDocument::FindWholeWords);//FindWholeWords全词匹配

    /*
    查找到的文本设置成高亮
    QPalette是Qt中的一个调色板类,用于美化界面。
    设置的方法是先调用QWidget::palette()获取当前面板,修改它为自定义的值后再
    通过方法QWidget::setPalette设置为新修改的面板
    */
            QPalette palette = ptextedit->palette();
    /*
    Highlight:高亮背景色
    控件的颜色状态分三种:(1)Active,激活状态 (2)Disabled,禁用状态 (3)Inactive,未激活状态
    */
            palette.setColor(QPalette::Highlight, palette.color(QPalette::Active,QPalette::Highlight));
            ptextedit->setPalette(palette);

            if(!ok)
            {
                QMessageBox::information(this, tr("查找"), tr("找不到%1").arg(str));
            }
            else
                QMessageBox::information(this, tr("查找"), tr("找到").arg(str));
        });

    });

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

扳手的海角

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值