创建对话框---QT

              今天说的关于QT的一个练习,写一个搜索对话框吧!好吧,直接进入主题吧!先建工程,需要注意的是在Base Dialog的选择的时候要选QDialog。其他的没什么了。名字的话就随便了,我起了个dialog。

        1、首先打开dialog.h,编写头文件,代码如下:

#ifndef DIALOG_H
#define DIALOG_H
 
#include <QtGui/QDialog>
 
 class QCheckBox;                     //声明四个用到的类
 class QLabel;
 class QLineEdit;
 class QPushButton;
 
 class FindDialog : public QDialog    //FindDialog继承QDialog这个类
 {
         Q_OBJECT                     //一个宏,凡是定义信号槽的类都必须声明这个宏
 
 public:
         FindDialog(QWidget *parent = 0);
         ~FindDialog();
 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_H
 
          2、再打开dialog.cpp文件,进行编写,代码如下: 

#include <QtGui>
 #include "dialog.h"       //把头文件引进来
 
 FindDialog::FindDialog(QWidget *parent): QDialog(parent) 
 {
         label = new QLabel(tr("Find &what:"));  //label传进来字符串
         lineEdit = new QLineEdit;       
         label->setBuddy(lineEdit);  
 
         caseCheckBox = new QCheckBox(tr("Match &case"));    //创建两个QCheckBox
         backwardCheckBox = new QCheckBox(tr("Search &backford"));
 
         findButton = new QPushButton(tr("&Find"));
         findButton->setDefault(true);         //这两行代码就是先把button设置为灰色不可用
         findButton->setEnabled(false);     
 
         closeButton = new QPushButton(tr("Close"));
         //接下来就是三个connect语句了,用来连接信号槽
 
         connect(lineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(enableFindButton(const QString&)));  //当lineEdit发出textChanged信号时,FindButton的enableFindButton函数就被调用
         connect(findButton, SIGNAL(clicked()), this, SLOT(findClicked()));
         connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
         //接下来就是layout的使用了,这个大家仔细琢磨琢磨就知道了
 
         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());
 }
 
 FindDialog::~FindDialog()
 {
 
 }
 
 void FindDialog::findClicked()
 {
         QString text = lineEdit->text();
         Qt::CaseSensitivity cs = caseCheckBox->isChecked() ? Qt::CaseInsensitive : Qt::CaseSensitive;
         if(backwardCheckBox->isChecked()) 
         {
                 emit findPrevious(text, cs);//如果backwardCheckBox被选中的话,就发出findPrevious信号
         }
         else {
                 emit findNext(text, cs);
         }
 }
 
 void FindDialog::enableFindButton(const QString &text)
 {
         findButton->setEnabled(!text.isEmpty());
 }
          3、最后就是main.cpp文件了,代码如下: 

#include <QApplication>
 
 #include "dialog.h"
 
 int main(int argc, char *argv[])
 {
         QApplication app(argc, argv);
         FindDialog *dialog = new FindDialog;
         dialog->show();
         return app.exec();
 }
 
最后的运行效果如下:
 
 
 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值