英文:
How to update a variable updated in c++ in qml?
问题
我知道如何使用Q_PROPERTY
来更新qml变量。
class _data: public QObject {
Q_OBJECT
public:
_data();
~_data();
Q_PROPERTY(int testflag READ getTestFlag WRITE setTestFlag NOTIFY testFlagChanged);
int m_testFlag;
auto getTestFlag() -> int {return m_testFlag;}
auto setTestFlag(int data) -> void {this->m_testFlag = data; emit testFlagChanged();}
signals:
void testFlagChanged();
}
使用上述C++代码来更新m_testFlag
变量时,必须强制调用testFlagChanged()
信号以更新qml中的值。
_data* mydata;
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QQuickView view;
mydata = new _data;
view.rootContext()->setContextProperty("mydata", mydata);
view.show();
app.installEventFilter(new myEventFilter(&view)); // 添加事件过滤器以在接收到键盘事件时操作Update()函数。我已经检查过它可以工作,所以我省略了分开的代码。
return app.exec();
}
void update() {
mydata->m_testFlag = 10;
mydata->testFlagChanged();
}
有没有一种更加优雅的方式来更新qml变量,而不必强制调用testFlagChanged()
信号呢?
我一直在寻找类似信号绑定的东西,但是我找不到可以使用的示例。我需要帮助。
编辑
我发布了一个带有测试代码的问题,但我没想到会受到如此多的批评。
我将在update()函数中进行修复,并更详细地说明我的问题。所以冷静下来,看一下代码。
我试图通过将其声明为data类中的public变量来在C++中更新m_testFlag变量。
在这种情况下,我希望在变量值更改时自动生成changed()信号,而不是由开发人员强制生成。有没有更好的方法?
英文:
I know how to update qml variables using Q_PROPERTY
.
class _data: public QObject {
Q_OBJECT
public:
_data();
~_data();
Q_PROPERTY(int testflag READ getTestFlag WRITE setTestFlag NOTIFY testFlagChanged);
int m_testFlag;
auto getTestFlag() -> int {return m_testFlag;}
auto setTestFlag(int data) -> void {this->m_testFlag = data; emit testFlagChanged();}
signals:
void testFlagChanged();
}
When updating the m_testFlag
variable in c++ using the code above, the testFlagChanged()
signal must be called forcibly to update the value in qml.
_data* mydata;
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QQuickView view;
mydata = new _data;
view.rootContext()->setContextProperty("mydata", data);
view.show();
app.installEventFilter(new myEventFilter(&view)); // Added an event filter to operate the Update() function when a keyboard event is received. I checked that it works, so I omitted the separate code.
return app.exec();
}
void update() {
mydata->m_testFlag = 10;
mydata->testFlagChanged();
}
Isn't there a way to update the qml variable in a more elegant way other than forcibly calling the testFlagChanged()
signal?
I've been looking for something like signal bind, but I haven't been able to find any examples I can use. I need help.
> Edit
I posted a question with test code, but I didn't expect it to receive so much criticism.
I will fix it in the function update() function and write more details about my question. So calm down and take a look at the code.
I am trying to update m_testFlag variable in c++ by declaring it as public in a class called data.
At this time, I want the changed() signal to be generated automatically when the variable value is changed, rather than forcibly generated by the developer. Is there any better way?
答案1
得分: 0
"不是有一种更加优雅的方式来更新qml变量,而不是强制调用testFlagChanged()信号吗?" -- "更加优雅的方式"可能是主观的,但我认为你只需要这样做,而不是这样:
mydata->m_testFlag = 10;
mydata->testFlagChanged();
可以这样调用:
mydata->setTextFlag(10);
此外,除非你有非常好的理由,不要将m_testFlag设为公共成员。它应该是私有的。这就是拥有getter/setter函数的整个目的,所以外部对象不需要知道类的内部细节。
英文:
"Isn't there a way to update the qml variable in a more elegant way other than forcibly calling the testFlagChanged() signal?" -- A "more elegant way" can be subjective, but I think all you need to do is instead of this:
mydata->m_testFlag = 10;
mydata->testFlagChanged();
call this:
mydata->setTextFlag(10);
Also, don't make m_testFlag public unless you have a really good reason to. It should be private. That's the whole purpose of having getter/setter functions, so outside objects don't need to know the internals of the class.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论