gpt4 book ai didi

c++ - QML使用带有参数的C++函数

转载 作者:行者123 更新时间:2023-12-01 14:43:25 26 4
gpt4 key购买 nike

我在应用程序中使用的是小部件,但由于外观原因我正迁移到qml,但我打算继续在banckground中使用C++

class Usuario : public QObject{

Q_OBJECT

Q_PROPERTY(bool login WRITE login)

public:

Usuario(QObject *parent = nullptr);
~Usuario();

static bool login(const QString& usuario, const QString& senha);

};

编译时出现错误,指示该函数未接收任何参数,那么如何在qml中调用登录函数?

最佳答案

您在混淆概念,Q_PROPERTY不能用于公开函数或方法,而是用于创建属性(更多信息here),您应该使用Q_INVOKABLE或Q_SLOT:

class Usuario : public QObject{
Q_OBJECT
public:
Usuario(QObject *parent = nullptr);
~Usuario();
Q_INVOKABLE static bool login(const QString& usuario, const QString& senha);
// or
// Q_SLOT static bool login(const QString& usuario, const QString& senha);
};

关于c++ - QML使用带有参数的C++函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60196109/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com