Skip to content

Instantly share code, notes, and snippets.

@lixingcong
Created April 24, 2019 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lixingcong/d4e96d84cb3f8b5eb67dc863b4b65732 to your computer and use it in GitHub Desktop.
Save lixingcong/d4e96d84cb3f8b5eb67dc863b4b65732 to your computer and use it in GitHub Desktop.
Q_INVOKABLE宏的简单用法
#include <QObject>
#include <QDebug>
class MyClass : public QObject
{
Q_OBJECT
public:
explicit MyClass(QObject *parent = nullptr):QObject(parent){}
Q_INVOKABLE void printHello(const QString& text)
{
qDebug()<<"MyClass:"<<text;
}
};
// ======== main.cpp ===========
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
auto myClass=new MyClass(&a);
QString str("main:");
QMetaObject::invokeMethod(myClass, "printHello", Qt::QueuedConnection, Q_ARG(QString,str));
return a.exec();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment