zoukankan      html  css  js  c++  java
  • 用QT写一个对话框

    打开QT creater创建取名去findDialog的项目,这个项目要基于QDialog。直接上FindDialog.h的头文件。

     1 #ifndef FINDDIALOG_H
     2 #define FINDDIALOG_H
     3 
     4 #include<QDialog>
     5 #include<QtGui>
     6 class QCheckBox;
     7 class QLabel;
     8 class QLineEdit;
     9 class QPushButton;
    10 
    11 class FindDialog : public QDialog
    12 {
    13         Q_OBJECT
    14         public:
    15         FindDialog(QWidget *parent = 0);
    16         ~FindDialog();
    17 
    18         signals:
    19         void findNext(const QString &str, Qt::CaseSensitivity cs);
    20         void findPrevious(const QString &str, Qt::CaseSensitivity cs);
    21         private slots:
    22         void findClicked();
    23         void enableFindButton(const QString &text);
    24         private:
    25         QLabel *label;
    26         QLineEdit *lineEdit;
    27         QCheckBox *caseCheckBox;
    28         QCheckBox *backwardCheckBox;
    29         QPushButton *findButton;
    30         QPushButton *closeButton;
    31 };
    32 
    33 #endif // FINDDIALOG_H

    头文件里,前向声明了几个类,这个类没有定义,在.cpp文件中,前向声明必须要 #include。这个未被定义的。不能直接定义一个对象,程序直接是错误的,而定义一个指针指向这个类是可以的,但是在析构的时候,因为这个指针指向的东西不明确,所以在析构的时候可能会导致内存泄露。

    下面直接上.cpp的文件,

     1 #include <QtGui>
     2 #include "finddialog.h"
     3 #include <QCheckBox>
     4 #include <QLabel>
     5 #include <QLineEdit>
     6 #include <QPushButton>
     7 #include <QHBoxLayout>
     8 FindDialog::FindDialog(QWidget *parent)
     9 : QDialog(parent)//调用父类的构造函数
    10 {
    11    label = new QLabel(tr("Find &what:"));
    12    lineEdit = new QLineEdit;
    13    label->setBuddy(lineEdit);
    14 
    15    caseCheckBox = new QCheckBox(tr("Match &case"));
    16    backwardCheckBox = new QCheckBox(tr("Search &backford"));
    17 
    18    findButton = new QPushButton(tr("&Find"));
    19    findButton->setDefault(true);
    20    findButton->setEnabled(false);
    21 
    22    closeButton = new QPushButton(tr("Close"));
    23 
    24    connect(lineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(enableFindButton(const QString&)));
    25    connect(findButton, SIGNAL(clicked()), this, SLOT(findClicked()));
    26    connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
    27 
    28       QHBoxLayout *topLeftLayout = new QHBoxLayout;
    29       topLeftLayout->addWidget(label);
    30       topLeftLayout->addWidget(lineEdit);
    31 
    32       QVBoxLayout *leftLayout = new QVBoxLayout;
    33       leftLayout->addLayout(topLeftLayout);
    34       leftLayout->addWidget(caseCheckBox);
    35       leftLayout->addWidget(backwardCheckBox);
    36 
    37       QVBoxLayout *rightLayout = new QVBoxLayout;
    38       rightLayout->addWidget(findButton);
    39       rightLayout->addWidget(closeButton);
    40       rightLayout->addStretch();
    41 
    42       QHBoxLayout *mainLayout = new
    43 
    44               QHBoxLayout;
    45                       mainLayout->addLayout(leftLayout);
    46                       mainLayout->addLayout(rightLayout);
    47                       setLayout(mainLayout);
    48 
    49                       setWindowTitle(tr("Find"));
    50                       setFixedHeight(sizeHint().height());
    51               }
    52 
    53               FindDialog::~FindDialog()
    54               {
    55 
    56               }
    57 
    58               void FindDialog::findClicked()
    59               {
    60                       QString text = lineEdit->text();
    61                       Qt::CaseSensitivity cs = caseCheckBox->isChecked() ? Qt::CaseInsensitive : Qt::CaseSensitive;
    62                       if(backwardCheckBox->isChecked()) {
    63                           emit findPrevious(text, cs);
    64                    } else {
    65                            emit findNext(text, cs);
    66                    }
    67            }
    68 
    69            void FindDialog::enableFindButton(const QString &text)
    70            {
    71                    findButton->setEnabled(!text.isEmpty());
    72            }

      Q_OBJECT这个宏在 定义信号的时候,都是要写的。

    最后直接上main.Cpp文件。

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

    最后运行结果。

  • 相关阅读:
    行转列函数listagg() WITHIN GROUP ()
    位图索引
    windows 杀掉进程
    vue 实践(过滤器)
    vue 总结
    vue v-show v-if 的使用
    vue v-for 绑定数据
    vue v-model实现数据的双向绑定
    vue .stop .self .capture .prevent 阻止冒泡
    vue v-on v-text 的运用
  • 原文地址:https://www.cnblogs.com/132818Creator/p/7211714.html
Copyright © 2011-2022 走看看