一.子类化QDialog
说一下学习感受,学习这个Qt子类化对话框其实有代码了,觉得挺方便的,执行一个qmake +项目文件生成与平台相关的Makefile文件,然后执行make编译源代码,生成与目标程序与执行文件。然后再执行可执行文件,对话框就跑出来了。很快很爽的做出来了。兴奋过后觉得自己也没学到什么呀!接着要深入了解程序,了解它的程序含义及程序运行原理。这是个designer的设计效果图。
从上面可以了解到,创建一个Find对话框,需要什么窗口部件。一个没有按钮的对话框(Dialog without Button,一个标签(label),一个行编辑器(lineEdit),两个复选框按钮(CheckBox),两个按键按钮(PushButton),一个垂直分隔符。
我的思路是这样的想把框架创建好,在建立信号和槽。
创建一个finddialog.h头文件
#ifndef FINDDIALOG_H
#define FINDDIALOG_H
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

#include <QDialog>
Class FINDDIALOG:public QDialog
{
       Public:
       FindDialog(QWidget *parent = 0);      
};

#endif
FindDialog的的头文件的建好了
创建一个finddialog.cpp C++源文件
 
#include <QtGui>
#include “finddialog.h”
FindDialog::FindDialog(QWidget *parent)
       :QDialog(parent)
{

 

}

 

内容先不写进去先。
创建一个main.cpp文件
#include <QApplication>
#include “finddialog.h”
int main(int argc,char *argv[])
{
       QApplication app(argc,argv);
       FindDialog *dialog = new FindDialog;
       Dialog->show();
       return app.exec();
}
运行效果是这样的:
接下来添加标签,行编辑器,在finddialog.h中定义两个类,分别是QLabelQLineEdit,定义两个私有对象分别为labellineEdit.然后再finddialog.cpp源文件中声明,建立labellineEdit两个对象的关系。定义一个水平布局管理器。管理labellineEdit两个窗口部件的大小和位置。
接着添加两个CheckBox,原理是一样的,在finddialog.h中定义一个复选框类,两个复选框对象。在finddialog.cpp中引用定义的复选框对象。运行看效果有点模样了
以同样的方式创建 Button 按钮,及布局管理器。运行看效果,成了。不过还有信号和槽连接
最后创建信号与槽私有的,当行编辑器中输入字符串是,就会调用私有的槽 enableFindButton const QString & .Find 按钮变成启用状态,当按下 Find 按钮就会调用 findClicked() 的私有槽。当用户单击 Close 时,对话框会关闭。 Close ()槽是从窗口部件 QWidget 类函数中继承而来的,并且它默认行为就是把窗口部件从用户的视野中隐藏起来(而无须将其删除)。在建立信号和槽连接时 QObject::connect ()应该是这样写的,但由于 QObject FindDialog 的父对象之一,因此可以省略 connect ()函数前面的 QObject ::前缀。最后运行代码。。代码:
finddialog.h 头文件
#ifndef FINDDIALOG_H
#define FINDDIALOG_H                                                                              
#include <QDialog>                                                                              
class QLabel;
class QLineEdit;
class QCheckBox;
class QPushCheckBox;
                                                                               
class FindDialog:public QDialog
{
        Q_OBJECT
                                                                                
        public:
        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.h源文件,要实现的功能都在这个文件里对象引用,窗口部件布局,信号和槽的链接等等。。

 
#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"));
                                                                              
        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::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.cpp 源文件
#include <QApplication>
#include "finddialog.h"                                                                    
int main(int argc,char *argv[])
{
        QApplication app(argc,argv);
        FindDialog *dialog = new FindDialog;
        dialog->show();
        return app.exec();
}
 
 
 
奋斗还在进行中....