如何告诉 qml 绑定附加依赖项?
How to tell a qml binding about additional dependencies?
我有一个 C++ 实现的 Quick Item,它提供了多个属性和 Q_INVOKABLE 具有 return 值的方法。这些方法中的大多数都依赖于这些属性。
我可以为方法定义通知信号吗?或者在绑定到它时,我可以添加额外的依赖项以便再次评估该方法吗?
在此示例中,我希望所有文本项在 theItem.something
更改时更新。
SimpleCppItem {
id: theItem
something: theSpinBox.value
}
RowLayout {
SpinBox { id: theSpinBox; }
Repeater {
model: 10
Text { text: theItem.computeWithSomething(index) }
}
}
SimpleCppItem
的实现如下所示:
class SimpleCppItem : public QQuickItem
{
Q_OBJECT
Q_PROPERTY(int something READ something WRITE setSomething NOTIFY somethingChanged)
public:
explicit SimpleCppItem(QQuickItem *parent = Q_NULLPTR) :
QQuickItem(parent),
m_something(0)
{ }
Q_INVOKABLE int computeWithSomething(int param)
{ return m_something + param; } //The result depends on something and param
int something() const { return m_something; }
void setSomething(int something)
{
if(m_something != something)
Q_EMIT somethingChanged(m_something = something);
}
Q_SIGNALS:
void somethingChanged(int something);
private:
int m_something;
};
函数无法做到这一点。但是有一些解决方法:
"Small Hack"(收到警告 M30:警告,请勿使用逗号表达式感谢 GrecKo,不再有警告!
Repeater {
model: 10
Text {
text: {theItem.something; return theItem.computeWithSomething(index);}
}
}
或者你用 "somethingChanged" 信号连接中继器中的每个项目:
Repeater {
model: 10
Text {
id: textBox
text: theItem.computeWithSomething(index)
Component.onCompleted: {
theItem.somethingChanged.connect(updateText)
}
function updateText() {
text = theItem.computeWithSomething(index)
}
}
}
===== 原始问题 =====
您可以像这样在 QML 文件中捕获信号:
SimpleCppItem {
id: theItem
something: theSpinBox.value
onSomethingChanged() {
consoloe.log("Catched: ",something)
//something ist the name of the parameter
}
}
我有一个 C++ 实现的 Quick Item,它提供了多个属性和 Q_INVOKABLE 具有 return 值的方法。这些方法中的大多数都依赖于这些属性。
我可以为方法定义通知信号吗?或者在绑定到它时,我可以添加额外的依赖项以便再次评估该方法吗?
在此示例中,我希望所有文本项在 theItem.something
更改时更新。
SimpleCppItem {
id: theItem
something: theSpinBox.value
}
RowLayout {
SpinBox { id: theSpinBox; }
Repeater {
model: 10
Text { text: theItem.computeWithSomething(index) }
}
}
SimpleCppItem
的实现如下所示:
class SimpleCppItem : public QQuickItem
{
Q_OBJECT
Q_PROPERTY(int something READ something WRITE setSomething NOTIFY somethingChanged)
public:
explicit SimpleCppItem(QQuickItem *parent = Q_NULLPTR) :
QQuickItem(parent),
m_something(0)
{ }
Q_INVOKABLE int computeWithSomething(int param)
{ return m_something + param; } //The result depends on something and param
int something() const { return m_something; }
void setSomething(int something)
{
if(m_something != something)
Q_EMIT somethingChanged(m_something = something);
}
Q_SIGNALS:
void somethingChanged(int something);
private:
int m_something;
};
函数无法做到这一点。但是有一些解决方法:
"Small Hack"(收到警告 M30:警告,请勿使用逗号表达式感谢 GrecKo,不再有警告!
Repeater {
model: 10
Text {
text: {theItem.something; return theItem.computeWithSomething(index);}
}
}
或者你用 "somethingChanged" 信号连接中继器中的每个项目:
Repeater {
model: 10
Text {
id: textBox
text: theItem.computeWithSomething(index)
Component.onCompleted: {
theItem.somethingChanged.connect(updateText)
}
function updateText() {
text = theItem.computeWithSomething(index)
}
}
}
===== 原始问题 =====
您可以像这样在 QML 文件中捕获信号:
SimpleCppItem {
id: theItem
something: theSpinBox.value
onSomethingChanged() {
consoloe.log("Catched: ",something)
//something ist the name of the parameter
}
}