谈到C++第一印象就是面向对象,但是从来都没有用明白多。由于平时从来都没用过C++,但是项目需要现在又要用到Qt,因此,不得不学习C++以及Qt的知识了。
学习了Qt一个类的封装,现在基本明白面向对象是什么意思了,面向对象不在乎就是封装类嘛,将对象的数据、行为等等封装在一起,但是,我们都知道不管是C++还是Qt都提供了各种基类这些。往往这些是不够的,所以面向对象的编程思想就是,运用现有的类封装自己的类,方便使用,就是C语言中常说的模块化思想。下面从一个Qt的实例中进行详细分析。想想,我们用Qt如果做一个查找的对话框,如下所示
首先,告诉大家的是这个对话框,我们可以用两三句话就可以做出来了,如下所示
#include <QApplication>
#include "finddialog.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
FindDialog *dialog = new FindDialog;
dialog->show();
return app.exec();
}
就是上面代码中红色的两行语言即可,是不是和我们用Qt做个Wdiget窗口是一样的呢?
答案肯定是一样的,但是Qt的标准库里面没有为我们提供FindDialog这个类呢? 因此,这就是我理解了面向对象编程的优势之处就是,我们应用Qt提供的标准类,根据面向对象的原理,什么继承,重载,信号与槽等等Qt的原理自己封装了一个FindDialog类,然后,在应用程序中应用这个类,那我们就相当容易了!!下面,详细介绍下,FindDialog类是如何进行封装的,我相信,经过了这个小测试,我们在以后编写面向对象的代码时就不会在是凌乱的了。
finddialog.h文件内容如下所示
#ifndef FINDDIALOG_H
#define FINDDIALOG_H
#include <QDialog> //Qt中对话框的基类,QDdialog从QWidget类中派生出来
class QCheckBox;
class QLabel;
class QLineEdit;
class QPushButton;
class FindDialog : public QDialog
{
Q_OBJECT //对于所有定义了信号和槽的类,在类定义开始处Q_OBJECT宏都是必需的
public:
FindDialog(QWidget *parent=0); //典型的Qt窗口部件类的定义方式。parent参数指定了它的父窗口部件。默认为空,意味着没有父对象
signals:
void findNext(const QString &str, Qt::CaseSensitivity cs); //find按键发出的两个信号
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
以上,就是FindDialog类的一个封装过程,包括了私有数据,信号与槽,即行为方式
下面看看这些行为是如何实现的,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"));
connect(lineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(enableFindButton(const QString &))); //如果lineEdit内容改变,那么findButton就有效
connect(findButton, SIGNAL(clicked()), this, SLOT(findClicked())); //findButton按下了,那么findClicked会执行
connect(closeButton, SIGNAL(clicked()), this, SLOT(close())); //closeButton按下了,那么会关闭窗口
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) //lineEdit内容该片,此函数会执行
{
findButton->setEnabled(!text.isEmpty());
}