1.信号槽在QObject对象 connect(第一个参数是传入对象,当对象执行什么样的操作,使用什么样的方法)。
或 connect(第一个参数是传入对象,当对象执行什么样的操作,传入一个新类,使用新类的方法)。
1.当按钮被点击时关闭窗口
QApplication a(argc, argv); QPushButton button("Quit"); QObject::connect(&button,&QPushButton::clicked,&QApplication::quit); button.show();2.newspaper.h
#include <QObject> // newspaper.h class Newspaper : public QObject { Q_OBJECT public: Newspaper(const QString & name) : m_name(name) { } void send() { emit newPaper(m_name); } signals: void newPaper(const QString &name); private: QString m_name; };reader.h
// reader.h #include <QObject> #include <QDebug> class Reader : public QObject { Q_OBJECT public: void receiveNewspaper(const QString & name) { qDebug() << "Receives Newspaper: " << name; } };main.cpp
QApplication a(argc, argv); Newspaper newspaper("Newspaper A"); Reader reader; QObject::connect(&newspaper, &Newspaper::newPaper, &reader,&Reader::receiveNewspaper); newspaper.send(); return a.exec();