元对象系统:创建了独立的软件组织,这些组件可以绑定在一起,但任何一个组件对于它所要连接的组件的情况事先都一无所知。
1.信号-槽
槽:和普通成员函数一样,可以是虚函数,可以被重载,可以是public,private,protected,并且可以被其它成员函数调用。唯一不同的是槽可以和信号连接,每当emit一个信号就会自动调用这个槽函数
emit:Qt中的关键字,像其它Qt扩展一样,它也会被c++预处理器转换成标准的c++代码
一般情况下信号和槽的必须有相同的顺序和相同的类型。
内省:对于实现信号和槽是必须的,并且允许应用程序的开发人员在运行时获得有关QObject子类的元信息,包括一个含有对象的类名以及它所支持的信号和槽的列表。
Qt通过提供一个独立的moc工具来解析O_OBJECT类的定义,由于moc使用纯C++来实现它的所有功能,所以Qt的元对象系统可以在任意C++编译器上工作。
1.Q_OBJECT宏声明了在每一个QObject子类中必须实现的一些内省函数:metaObject(),tr(),qt_metacall()...
2.moc工具生成了用于Q_OBJECT声明的所有函数和所有信号的实现
3.像connect()和disconnect()这样的OBject的成员函数使用这些内省函数来完成它们的工作。
findDialog.h
findDialog.cpp
[img]http://dl.iteye.com/upload/attachment/506044/eb822063-8ea0-3aa3-93fe-dbaad7c452e8.png[/img]
1.信号-槽
槽:和普通成员函数一样,可以是虚函数,可以被重载,可以是public,private,protected,并且可以被其它成员函数调用。唯一不同的是槽可以和信号连接,每当emit一个信号就会自动调用这个槽函数
emit:Qt中的关键字,像其它Qt扩展一样,它也会被c++预处理器转换成标准的c++代码
一般情况下信号和槽的必须有相同的顺序和相同的类型。
内省:对于实现信号和槽是必须的,并且允许应用程序的开发人员在运行时获得有关QObject子类的元信息,包括一个含有对象的类名以及它所支持的信号和槽的列表。
Qt通过提供一个独立的moc工具来解析O_OBJECT类的定义,由于moc使用纯C++来实现它的所有功能,所以Qt的元对象系统可以在任意C++编译器上工作。
1.Q_OBJECT宏声明了在每一个QObject子类中必须实现的一些内省函数:metaObject(),tr(),qt_metacall()...
2.moc工具生成了用于Q_OBJECT声明的所有函数和所有信号的实现
3.像connect()和disconnect()这样的OBject的成员函数使用这些内省函数来完成它们的工作。
findDialog.h
#ifndef FINDDIALOG_H
#define FINDDIALOG_H
#include<QDialog>
class QLabel;
class QHBoxLayout;
class QLineEdit;
class QVBoxLayout;
class QRadioButton;
class QCheckBox;
class QPushButton;
class FindDialog:public QDialog{
Q_OBJECT
public:
explicit FindDialog(QWidget *parent=0);
private:
QLabel* label1;
QLineEdit* lineEdit;
QHBoxLayout* layout;
QHBoxLayout* layout1;
QVBoxLayout* layout2;
QVBoxLayout* layout3;
QVBoxLayout* layout4;
QRadioButton* radioButton;
QCheckBox* checkBox;
QPushButton* findButton;
QPushButton* closeButton;
protected slots:
void clearText(QString s);
void findSlots(const QString& s);
void responseClick();
signals:
void findSignals(const QString& s);
};
#endif // FINDDIALOG_H
findDialog.cpp
#include"FindDialog.h"
#include<QApplication>
#include<QHBoxLayout>
#include<QVBoxLayout>
#include<QLabel>
#include<QLineEdit>
#include<QRadioButton>
#include<QCheckBox>
#include<QPushButton>
#include<QMessageBox>
static const QString lineStr = "write...";
FindDialog::FindDialog(QWidget *parent)
:QDialog(parent)
{
label1 = new QLabel("Find What:");
layout = new QHBoxLayout;
layout1 = new QHBoxLayout;
layout2 = new QVBoxLayout;
layout3 = new QVBoxLayout;
layout4 = new QVBoxLayout;
lineEdit = new QLineEdit;
lineEdit->setText(lineStr);
radioButton = new QRadioButton("Match case");
radioButton->setChecked(true);
checkBox = new QCheckBox("Search backward");
checkBox->setChecked(true);
findButton = new QPushButton("Find");
closeButton = new QPushButton("Close");
layout1->addWidget(label1);
layout1->addWidget(lineEdit);
layout2->addWidget(radioButton);
layout2->addWidget(checkBox);
layout3->addLayout(layout1);
layout3->addLayout(layout2);
layout4->addWidget(findButton);
layout4->addWidget(closeButton);
layout->addLayout(layout3);
layout->addLayout(layout4);
setLayout(layout);
setWindowTitle("Find");
sizeHint();
connect(closeButton,SIGNAL(clicked()),this,SLOT(close()));
connect(lineEdit,SIGNAL(textEdited(QString)),this,SLOT(clearText(QString)));
connect(findButton,SIGNAL(clicked()),this,SLOT(responseClick()));
connect(this,SIGNAL(findSignals(const QString&)),this,SLOT(findSlots(const QString&)));
}
//当第一次编辑lineEdit时把原来的write...去掉,并解除这对信号-槽
void FindDialog::clearText(QString s)
{
s.remove(lineStr);
lineEdit->clear();
disconnect(lineEdit,SIGNAL(textEdited(QString)),this,SLOT(clearText(QString)));
lineEdit->setText(s);
}
//响应点击,发出不同的信号
void FindDialog::responseClick()
{
if(radioButton->isChecked()||checkBox->isChecked()){
emit findSignals("checked");
}else{
emit findSignals("not checked");
}
}
void FindDialog::findSlots(const QString &s)
{
QMessageBox* msg = new QMessageBox(QMessageBox::Information,
"Message",
s);
msg->exec();
}
int main(int argc,char* argv[])
{
QApplication app(argc,argv);
FindDialog* dialog = new FindDialog;
dialog->show();
app.exec();
}
[img]http://dl.iteye.com/upload/attachment/506044/eb822063-8ea0-3aa3-93fe-dbaad7c452e8.png[/img]