Transfer complex data structures (such as structures) between C ++ and QML documents

The following structures are available:
typedef struct
{
pid_t pid;
char logical_name [AMS_MAX_APP_NAME_LEN];
char full_name [AMS_MAX_APP_NAME_LEN];
AMS_AppState_e state;
int kbd_fd;
} AMS_AppInfo_t;

用C++封装一个类如下:
class AppInfo : public QObject
{
Q_OBJECT
Q_PROPERTY(int pid READ read_pid WRITE write_pid)
Q_PROPERTY(QString logical_name READ read_logical_name WRITE write_logical_name)
Q_PROPERTY(QString full_name READ read_full_name WRITE write_full_name)
Q_PROPERTY(AppState state READ read_state WRITE write_state)
Q_PROPERTY(int kbd_fd READ read_kbd WRITE write_kbd)

public:
AppInfo(QObject *parent = 0);

int read_pid() const;
void write_pid(int pid);

QString read_logical_name() const;
void write_logical_name(const QString &name);

QString read_full_name() const;
void write_full_name(const QString &name);

enum AppState { IDLE, ACTIVE, STOPPED };
AppState read_state() const;
void write_state(AppState state);
Q_ENUMS(AppState)

int read_kbd() const;
void write_kbd(int kbd_fd);

private:
int m_pid;
QString m_logical_name;
QString m_full_name;
AppState m_state;
int m_kbd_fd;
};

Set invokable function
class CAmsClient: public QObject

{
    Q_OBJECT

public:
    CAmsClient(QObject *parent = 0);

Q_INVOKABLE AppInfo *read_appinfo(const QString &app_name);
};

AppInfo *CAmsClient::read_appinfo(const QString &app_name)
{

AppInfo *appinfo = new AppInfo;
appinfo->write_pid(1);
appinfo->write_logical_name(app_name);
appinfo->write_full_name(“amsclient full name”);
appinfo->write_state(AppInfo::ACTIVE);
appinfo->write_kbd(10);

return appinfo;
}

Register the above two classes in the declaration so that you can use
qmlRegisterType <AppInfo> (uri, 1, 0, “AppInfo”) in
qml ; qmlRegisterType <CAmsClient> (uri, 1, 0, “Ams”);
QML_DECLARE_TYPE ( AppInfo) // Global declaration
QML_DECLARE_TYPE (CAmsClient)

We call
Q_EXPORT_PLUGIN2 (qmlamsplugin, AmsPlugin) in qml as a plugin

In qml javascript makes the following calls:

var appinfo = ams.read_appinfo(“stb.qml”);
console.log(“app pid: ” + appinfo.pid);
console.log(“app logical name: ” + appinfo.logical_name);
console.log(“app state: ” + appinfo.state);
console.log(“app full name: ” + appinfo.full_name);
console.log(“app kbd: ” + appinfo.kbd_fd);

Wow
Published 206 original articles · praised 18 · 70,000 views

Guess you like

Origin blog.csdn.net/lvmengzou/article/details/105298465