首页 > 代码库 > Creating Dialogs

Creating Dialogs

技术分享
 1 #ifndef DIALOG_H 2 #define DIALOG_H 3  4 #include <QtWidgets> 5 //#include <QDialog> 6 //#include <QLabel> 7 //#include <QCheckBox> 8 //#include <QLineEdit> 9 //#include <QPushButton>10 //#include <QtCore>11 namespace Ui {12 class Dialog;13 }14 15 class Dialog : public QDialog16 {17     Q_OBJECT18 19 public:20     explicit Dialog(QWidget *parent = 0);21     ~Dialog();22 23 signals:24     void FindNext(const QString &str, Qt::CaseSensitivity cs);25     void FindPre (const QString &str, Qt::CaseSensitivity cs);26 27 private slots:28     void FindClicked();29     void EnabledFindButton(const QString &text);30 31 private:32     Ui::Dialog *ui;33     QLabel *label;34     QLineEdit *lineEdit;35     QCheckBox *caseCheckBox;36     QCheckBox *backwardCheckBox;37     QPushButton *findButton;38     QPushButton *closeButton;39 };40 41 #endif // DIALOG_H
dialog.h
技术分享
#include "dialog.h"#include "ui_dialog.h"Dialog::Dialog(QWidget *parent) :    QDialog(parent),    ui(new Ui::Dialog){    ui->setupUi(this);    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"));    connect(lineEdit,SIGNAL(textChanged(const QString &)),this,SLOT(EnabledFindButton(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 Dialog::FindClicked(){    QString text = lineEdit->text();    Qt::CaseSensitivity cs = caseCheckBox->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive;    if(backwardCheckBox->isChecked())    {        emit FindPre(text,cs);    }    else        emit FindNext(text,cs);}void Dialog::EnabledFindButton(const QString &text){    findButton->setEnabled(!text.isEmpty());}Dialog::~Dialog(){    delete ui;}
dialog.cpp
技术分享
#include "dialog.h"#include <QApplication>int main(int argc, char *argv[]){    QApplication a(argc, argv);    Dialog w;    w.show();    return a.exec();}
main.cpp

1、对于定义了信号(signals)或槽(slots)的类,宏定义Q_OBJECT是必须的

2、tr(),翻译可显字符

3、‘&’表示快捷键

4、setDefault(true) 设置default按钮(按回车即为按下该按钮)

5、setEnabled(false) 设置按钮无效,按钮显示为灰色

6、setWindowTitle(tr("Find"));

     setFixedHeight(sizeHint().height()); //程序自己设置一个合适的高度

7、connect(sender,SIGNAL(signal),receiver,SLOT(slot));

QLabel *label = new QLabel;QScrollBar *scrollBar = new QScrollBar;QObject::connect(scrollBar, SIGNAL(valueChanged(int)),                 label,  SLOT(setNum(int)));

  This example ensures that the label always displays the current scroll bar value. Note that the signal and slots parameters must not   contain any variable names, only the type. E.g. the following would not work and return false:

// WRONGQObject::connect(scrollBar, SIGNAL(valueChanged(int value)),                 label, SLOT(setNum(int value)));

  a signal can be connected to another signal

connect(lineEdit, SIGNAL(textChanged(const QString &)), this, SIGNAL(updateRecord(const QString &)));

  when the first signal is emitted, the second signal is emitted as well. 

  To successfully connect a signal to a slot (or to another signal), they must have the same parameter types in the same order:

connect(ftp, SIGNAL(rawCommandReply(int, const QString &)), this , SLOT(processReply(int, const QString &)));

 

Creating Dialogs