C++ 与QML消息交互

        在QML编程中,我们肯定是需要与C++进行信息交互的。本文以个人项目中使用的实例来进行讲述QML和C++信息交互。

        QML和C++间的信息交互有很多方式,本文主要以信号和槽来进行交互。在本项目中,我们主要定义一个类来进行QML和C++间的信息交互,C++通过信号的方式来把信息传递给QML,QML通过槽函数来把信息传递给C++. 下面为具体的代码。

MsgInterface.h
/* c++和QML间消息交互*/
#ifndef __MSG_INTERFACE_H__
#define __MSG_INTERFACE_H__
#include<QObject>
#include<QJsonObject>
#include<mutex>
class MsgInteractive : public QObject
{
    Q_OBJECT
    public:
    MsgInteractive ();
    ~MsgInteractive ();
    //获取单例对象
    static MsgInteractive* GetSingletonObj();
    signals:
    //C++ 通过信号传递信息给QML
    void allSignalToQml(unsigned char index,QJsonObject msg);
    public slots:
    //QML通过槽函数传递信息给C++
    void allInfoToC(unsigned char index,QJsonObject msg);
    private:
    static MsgInteractive* s_obj;
    static std::mutex s_mutex;
};
#endif
 

猜你喜欢

转载自blog.csdn.net/dreamliweiming/article/details/132322502