C++是面向对象的语言,即封装类——将对象的数据、行为等等封装在一起。
C++和Qt都提供了基类,但在使用过程中往往是不够的,因此,面向对象的编程思想是:运用现有的类封装自己的类,以便使用,也是C语言中的模块化思想。
举例说明:
查找对话框
main.cpp
#include "finddialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
FindDialog *dialog=new FindDialog;
dialog->show();
return app.exec();
}
因为Qt中没有finddialog类,所有需要利用基类来进行封装,之后再再应用程序中使用这个类。
finddialog.h
/***finddialog类的封装过程***/
#ifndef FINDDIALOG_H
#define FINDDIALOG_H
#include <QDialog> //Qt中对话框的基类,QDialog从QWidget类中派生出来
#include <QCheckBox>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
class QCheckBox; //复选框
class QLabel;
class QLineEdit;
class QPushButton;
class FindDialog : public QDialog
{
Q_OBJECT //对于所有定义了信号和槽的类,都要在类定义的开始处声明Q_OBJECT宏
public