Qt入门(常用类、方法、信息槽)

常用类

QApplication a(argc, argv); //实例化一个应用 QLabel label(“Hello world”); //标签 QPushButton button(“Quit”); //button MainWindow b;b.show(); //窗口

常用方法

QObject::connect()有五个重载 QMetaObject::Connection connect(const QObject *, const char *,const QObject *, const char *,Qt::ConnectionType); QMetaObject::Connection connect(const QObject *, const QMetaMethod &,const QObject *, const QMetaMethod &,Qt::ConnectionType); QMetaObject::Connection connect(const QObject *, const char *,const char *,Qt::ConnectionType) const; QMetaObject::Connection connect(const QObject *, PointerToMemberFunction,const QObject *, PointerToMemberFunction,Qt::ConnectionType) QMetaObject::Connection connect(const QObject *, PointerToMemberFunction,Functor);

注:Functor类型:这个类型可以接受 static 函数、全局函数以及 Lambda 表达式。

qDebug()<<“” //类似cout的标准输出

信息槽

信息槽机制,通过QObject::connect()函数实现。

自定义信息槽。

注意:

(1)只有继承了QObject类的类,才具有信号槽的能力

(2)凡是QObject类(不管是直接子类还是间接子类),都应该在第一行代码写上Q_OBJECT,这个宏应该放在头文件中。

(3)实例

//!!! Qt5 #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 #include <QObject> #include <QDebug> class Reader : public QObject { Q_OBJECT public: Reader() {} void receiveNewspaper(const QString & name) { qDebug() << “Receives Newspaper: ” << name; } }; ////////// main.cpp #include <QCoreApplication> #include “newspaper.h” #include “reader.h” int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); Newspaper newspaper(“Newspaper A”); Reader reader; QObject::connect(&newspaper, &Newspaper::newPaper, &reader, &Reader::receiveNewspaper); newspaper.send(); return app.exec(); }
<

原文:https://blog.csdn.net/u014451076/article/details/51125338

免责声明:文章内容来自互联网,本站不对其真实性负责,也不承担任何法律责任,如有侵权等情况,请与本站联系删除。
转载请注明出处:Qt入门(常用类、方法、信息槽) https://www.yhzz.com.cn/a/14975.html

上一篇 2023-05-12
下一篇 2023-05-12

相关推荐

联系云恒

在线留言: 我要留言
客服热线:400-600-0310
工作时间:周一至周六,08:30-17:30,节假日休息。