Qt对话框QDialog之查找对话框

一、查找对话框UI

代码如下(示例):

#ifndef FINDDIALONG_H
#define FINDDIALONG_H
#include <QDialog>

class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;

class FindDIalong:public QDialog
{
    Q_OBJECT

public:

    explicit FindDIalong(QWidget *parent = 0);

signals:

    void signalFindNext(const QString &str,Qt::CaseSensitivity);
    void signalFindPre(const QString &str,Qt::CaseSensitivity);

private slots:

    void slotFindClick();
    void slotEnableFindButton(const QString &text);

private:

    QLabel *mPtr_label;
    QLineEdit *mPtr_lineEdit;
    QCheckBox *mPtr_caseCheckBox;
    QCheckBox *mPtr_backwrdCheckBox;
    QPushButton *mPtr_findButton;
    QPushButton *mPtr_closeButton;

};

#endif // FINDDIALONG_H

代码如下(示例):

#include "find_dialong.h"
#include <QtGui>

FindDIalong::FindDIalong(QWidget *parent):QDialog(parent)
{
	//&表示快捷键,也可以控制焦点
    mPtr_label = new QLabel(tr("Find &what:"));
    mPtr_lineEdit = new QLineEdit;
    //添加伙伴,当mPtr_label按下快捷键的时候,接受焦点至mPtr_lineEdit
    mPtr_label->setBuddy(mPtr_lineEdit);

    mPtr_caseCheckBox = new QCheckBox(tr("Match &case"));
    mPtr_backwrdCheckBox = new QCheckBox(tr("Search &backward"));

    mPtr_findButton = new QPushButton("&Find");
    mPtr_findButton->setDefault(true);
    mPtr_findButton->setEnabled(false);

    mPtr_closeButton = new QPushButton(tr("close"));

    connect(mPtr_lineEdit,SIGNAL(textChanged(const QString&)),this,SLOT(slotEnableFindButton(const QString&)));
    connect(mPtr_findButton,SIGNAL(clicked()),this,SLOT(slotFindClick()));
    connect(mPtr_closeButton,SIGNAL(clicked()),this,SLOT(close()));

    QHBoxLayout *topLeftLayout =new QHBoxLayout;
    topLeftLayout->addWidget(mPtr_label);
    topLeftLayout->addWidget(mPtr_lineEdit);

    QVBoxLayout *leftLayout = new QVBoxLayout;
    leftLayout->addWidget(mPtr_caseCheckBox);
    leftLayout->addWidget(mPtr_backwrdCheckBox);
    leftLayout->addLayout(topLeftLayout);

    QVBoxLayout *reftLayout = new QVBoxLayout;
    reftLayout->addWidget(mPtr_findButton);
    reftLayout->addWidget(mPtr_closeButton);
    ///添加分隔符,即小弹簧
    reftLayout->addStretch();

    QHBoxLayout *mainLayout =new QHBoxLayout;
    mainLayout->addLayout(leftLayout);
    mainLayout->addLayout(reftLayout);
    setLayout(mainLayout);
	
	///sizeHint().height() 自动生成一个理想大小的尺寸
    setFixedHeight(sizeHint().height());
}

void FindDIalong::slotFindClick()
{
    QString text = mPtr_lineEdit->text();
    ///Qt::CaseSensitivity 大小写敏感
    Qt::CaseSensitivity cs = mPtr_caseCheckBox->isChecked() ? Qt::CaseSensitive:Qt::CaseInsensitive;
    if(mPtr_backwrdCheckBox->isChecked())
    {
        emit signalFindNext(text,cs);
    }
    else
    {
        emit signalFindPre(text,cs);
    }
}

void FindDIalong::slotEnableFindButton(const QString &text)
{
    mPtr_findButton->setEnabled(!text.isEmpty());
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值