QT-创建对话框

#ifndef FINDDIALOG_H
#define FINDDIALOG_H

#include<QDialog>

//前置声明会告诉C++编译程序类的存在,而不用提供类定义中的所有细节。
class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;

//FindDialog 继承 QDialog(对话框)类
class FindDialog : public QDialog{
        Q_OBJECT//对于所有定义了信号和槽的类,在类定义开始处的Q_OBJECT宏都是必需的。
public:
    //构造函数  参数为:QWidget部件类
    FindDialog(QWidget *parent = 0);

    //signals部分声明了当用户点击Find按钮时对话框所发射的两个信号
signals:
    //Qt::CaseSensitivity为枚举型,可取值Qt::CaseSensitive和Qt::CaseInsensitive
    //表示匹配的灵敏度
    void findNext(const QString &str,Qt::CaseSensitivity cs);
    void findPrevious(const QString &str,Qt::CaseSensitivity cs);

private slots:
    void findClicked();
    void enableFindButton(const QString &text);

private:
    //因为都是指针,所以可以使用前置类型。
    QLabel *label;
    QLineEdit *lineEdit;
    QCheckBox *backwardCheckBox;
    QCheckBox *caseCheckBox;
    QPushButton *findButton;
    QPushButton *closeButton;

};
#endif // FINDDIALOG_H

上面是头文件

finddialog.h
下面是
finddialog.cpp
#include<QPushButton>
#include<QLabel>
#include<QHBoxLayout>
#include<QVBoxLayout>
#include<QCheckBox>
#include<QLineEdit>
#include"finddialog.h"

FindDialog::FindDialog(QWidget *parent)
    :QDialog(parent)
{
    //创建了一个带有快捷键ALT+W的标签
    label = new QLabel(tr("Find &what:"));

    lineEdit = new QLineEdit;

    //设置编辑器作为标签的伙伴。按下标签的快捷键时,lineEdit会接受焦点。
    label->setBuddy(lineEdit);

    caseCheckBox = new QCheckBox(tr("Match &case"));

    backwardCheckBox = new QCheckBox(tr("Search &backward"));

    //Alt+F快捷键会激活Find按钮
    findButton = new QPushButton(tr("&Find"));

    //将Find按钮作为对话框的默认按钮。默认按钮就是当用户按下Enter键时能够按下对应的按钮
    findButton->setDefault(true);
    //将Find按钮禁止
    findButton->setEnabled(false);

    closeButton = new QPushButton(tr("Close"));

    //当lineEdit里面的内容发生变化时,会调用本类的enableFindButton函数。
    QObject::connect(lineEdit,SIGNAL(textChanged(const QString&)),this,SLOT(enableFindButton(const QString &)));

    //当点击Find按钮时,会调用本类的findClicked方法.
    QObject::connect(findButton,SIGNAL(clicked()),this,SLOT(findClicked()));

    //当点击Close按钮时,会调用本类或者父类的close方法
    QObject::connect(closeButton,SIGNAL(clicked()),this,SLOT(close()));

    //水平布局  加入部件label  lineEdit
    QHBoxLayout *topLeftLayout = new QHBoxLayout;
    topLeftLayout->addWidget(label);
    topLeftLayout->addWidget(lineEdit);

    //垂直布局  加入水平布局里所有的控件,接着加入caseCheckBox
    QVBoxLayout *leftLayout = new QVBoxLayout;
    leftLayout->addLayout(topLeftLayout);
    leftLayout->addWidget(caseCheckBox);

    //垂直布局 加入两个按钮部件
    QVBoxLayout *rightLayout = new QVBoxLayout;
    rightLayout->addWidget(findButton);
    rightLayout->addWidget(closeButton);
    //实现布局器的空白控件分配
    rightLayout->addStretch();

    //水平布局 加入之前布局的所有控件
    QHBoxLayout *mainLayout = new QHBoxLayout;
    mainLayout->addLayout(leftLayout);
    mainLayout->addLayout(rightLayout);
    //实现该控件的布局
    setLayout(mainLayout);

    setWindowTitle("Find");
    setFixedHeight(sizeHint().height());

}
void FindDialog::findClicked(){

        QString text = lineEdit->text();
        Qt::CaseSensitivity cs =
                  caseCheckBox->isChecked()?Qt::CaseSensitive:Qt::CaseInsensitive;

        if(backwardCheckBox->isChecked()){
            emit findPrevious(text,cs);

        }else{
            emit findNext(text,cs);
        }

}
void FindDialog::enableFindButton(const QString &text){
    findButton->setEnabled(!text.isEmpty());
}

main文件

1 #include "mainwindow.h"
2 #include <QApplication>
3 #include"finddialog.h"
4 int main(int argc,char *argv[]){
5     QApplication app(argc,argv);
6     FindDialog *dialog = new FindDialog;
7     dialog->show();
8     return app.exec();
9 }

 

posted @ 2016-10-27 11:19  IT男汉  阅读(1217)  评论(2编辑  收藏  举报