gpt4 book ai didi

c++ - 当从 C++ 发出的信号将数据发送到 QML

转载 作者:行者123 更新时间:2023-11-28 06:50:28 27 4
gpt4 key购买 nike

我该怎么做:当信号完成回复时(来自 c++)发送变量 replydata(来自 c++)到 TextArea(qml)

我如何连接它?也许 Q_PROPERTY 是个好方法?我使用 Qt 5.3

    QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));

SendGetSMS *Connection = new SendGetSMS();


engine.rootContext()->setContextProperty("abc1", Connection);

QObject::connect(Connection,&SendGetSMS::finishedReply,engine,...);

最佳答案

来自 the documentation

在 C++ 中:

class Message : public QObject
{
Q_OBJECT
Q_PROPERTY(QString author READ author WRITE setAuthor NOTIFY authorChanged)
public:
void setAuthor(const QString &a) {
if (a != m_author) {
m_author = a;
emit authorChanged();
}
}
QString author() const {
return m_author;
}
private:
QString m_author;
};

Message msg;
engine.rootContext()->setContextProperty("msg", &msg);

在 qml 中:

Text {
width: 100; height: 100
text: msg.author // invokes Message::author() to get this value

Component.onCompleted: {
msg.author = "Jonah" // invokes Message::setAuthor()
}
}

关于c++ - 当从 C++ 发出的信号将数据发送到 QML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24056110/

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