Welcome 微信登录

首页 / 操作系统 / Linux / Qt下完全手写创建对话框

在这里将完全使用C++编写一个查找对话框。首先来看它的头文件/////finddialog.h///////#ifndef FINDDIALOG_H
#define FINDDIALOG_H#include <QDialog>///前置申明,避免包含大的头文件使得编译能过快速通过class QCheckBox;class QLabel;
class QLineEdit;
class QPushButton;///定义自己的类,继承QDialog从而可以用其成员函数////并且具备相应的属性class FindDialog : public QDialog
{/////对于所有定义了信号和槽的类,定义下面这个宏是必须的
    Q_OBJECTpublic:
    FindDialog(QWidget *parent = 0);signals:
    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 *caseCheckBox;
    QCheckBox *backwardCheckBox;
    QPushButton *findButton;
    QPushButton *closeButton;
};#endif在来看看finddialog.cpp,在这个文件中主要实现了三个成员函数:构造函数,两个槽函数:在构造函数中实现了对话框的布局与信号的连接:#include <QtGui>#include "finddialog.h"FindDialog::FindDialog(QWidget *parent)  : QDialog(parent)
{/////创建窗口子部件并进行相应的属性设置
    label = new QLabel(tr("Find &what:"));
    lineEdit = new QLineEdit;
    label->setBuddy(lineEdit);    caseCheckBox = new QCheckBox(tr("Match &case"));
    backwardCheckBox = new QCheckBox(tr("Search &backward"));    findButton = new QPushButton(tr("&Find"));
    findButton->setDefault(true);
    findButton->setEnabled(false);    closeButton = new QPushButton(tr("Close"));//////将信号和槽连接起来并且在运行起来后系统会自动监视是否有指定的信号产生,如:文本框里是否有输入,/////button是否被点击,如果信号发生那么就调用相应的槽函数去处理。    connect(lineEdit, SIGNAL(textChanged(const QString &)),this, SLOT(enableFindButton(const QString &)));
    connect(findButton, SIGNAL(clicked()), this, SLOT(findClicked()));
    connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));/////对窗口进行布局    QHBoxLayout *topLeftLayout = new QHBoxLayout;
    topLeftLayout->addWidget(label);
    topLeftLayout->addWidget(lineEdit);    QVBoxLayout *leftLayout = new QVBoxLayout;
    leftLayout->addLayout(topLeftLayout);
    leftLayout->addWidget(caseCheckBox);
    leftLayout->addWidget(backwardCheckBox);    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(tr("Find"));
    setFixedHeight(sizeHint().height());
}  ////槽函数的实现,当点击时void FindDialog::findClicked()
{
    QString text = lineEdit->text();
    Qt::CaseSensitivity cs =
            caseCheckBox->isChecked() ? Qt::CaseSensitiv: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.cpp,在main函数里主要是实例化一个finddialog然后等待信号以执行相应的操作#include <QApplication>#include "finddialog.h"int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    FindDialog *dialog = new FindDialog;
    dialog->show(); ////将应用和系统事件传递给fingdialog对话框,并且等待程序结束进而退出
    return app.exec();
}