QML调用C++

//Login.h

#include <QObject>
#include <QDebug>

class Login : public QObject
{
    Q_OBJECT

public:

    explicit Login(QObject * parent = 0);

    Q_INVOKABLE void loginMessage(QString userName, QString passWord);
};
//Login.cpp

#include "Login.h"

Login::Login(QObject *parent) : QObject(parent)
{

}

void Login::loginMessage(QString userName, QString passWord)
{
    qDebug() << "loginMessage" << userName << passWord;
}
//main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>

#include "Login.h"

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    Login login;
    engine.rootContext()->setContextProperty("LOGIN",&login);
    engine.load(QUrl(QLatin1String("qrc:/main.qml")));

    return app.exec();
}
posted @ 2019-03-13 20:00  thomas_blog  阅读(206)  评论(0编辑  收藏  举报