【发布时间】:2015-07-16 05:43:15
【问题描述】:
我正在对我的班级进行非常简短(和部分)的描述,以显示我的问题。基本上我已经设置了两个属性。
class Fruit : public QObject
{
Q_OBJECT
....
public:
Q_PROPERTY( int price READ getPrice NOTIFY priceChanged)
Q_PROPERTY(Fruit * fruit READ fruit WRITE setFruit NOTIFY fruitChanged)
}
在我的 QML 中,如果我访问 price 属性,它运行良好且良好。但是,如果我访问显然返回Fruit 的fruit 属性,然后尝试使用它的price 属性,那将不起作用。这不应该以这种方式工作吗?
Text {
id: myText
anchors.centerIn: parent
text: basket.price // shows correctly
//text: basket.fruit.price // doesn't show
}
第二个返回Fruit,它也是一个属性,它有price 属性,但它似乎没有访问该属性?这应该工作吗?
更新
我包括我的源代码。我用HardwareComponent 创建了一个新的演示,这样更有意义。我试图根据收到的答案使其工作,但没有运气。
HardwareComponent 类是Computer 和CPU 的基类。
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
class HardwareComponent : public QObject
{
Q_OBJECT
public:
HardwareComponent() : m_price(0) {}
virtual int price() = 0;
virtual Q_INVOKABLE void add(HardwareComponent * item) { m_CPU = item; }
HardwareComponent * getCPU() const { return m_CPU; }
Q_SLOT virtual void setPrice(int arg)
{
if (m_price == arg) return;
m_price = arg;
emit priceChanged(arg);
}
Q_SIGNAL void priceChanged(int arg);
protected:
Q_PROPERTY(HardwareComponent * cpu READ getCPU);
Q_PROPERTY(int price READ price WRITE setPrice NOTIFY priceChanged)
HardwareComponent * m_CPU;
int m_price;
};
class Computer : public HardwareComponent
{
Q_OBJECT
public:
Computer() { m_price = 500; }
int price() { return m_price; }
void setprice(int arg) { m_price = arg; }
};
class CPU : public HardwareComponent
{
Q_OBJECT
public:
CPU() { m_price = 100; }
int price() { return m_price; }
void setprice(int arg) { m_price = arg; }
};
int main(int argc, char *argv[])
{
HardwareComponent * computer = new Computer;
CPU * cpu = new CPU;
computer->add( cpu );
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
engine.rootContext()->setContextProperty("computer", computer);
return app.exec();
}
#include "main.moc"
main.qml
import QtQuick 2.3
import QtQuick.Window 2.2
Window {
visible: true
width: 360
height: 360
Text {
anchors.centerIn: parent
text: computer.price // works
//text: computer.cpu.price // doesn't work, doesn't show any value
}
}
这是我所有项目文件的完整源代码。
当我运行它时,我得到这个输出:
启动 C:\Users\User\Documents\My Qt Projects\build-hardware-Desktop_Qt_5_4_0_MSVC2010_OpenGL_32bit-Debug\debug\hardware.exe ... QML 调试已启用。仅在安全的环境中使用。 qrc:/MainForm.ui.qml:20: ReferenceError: computer is not defined
即使它在第 20 行 (computer.price) 行发出警告,它仍然有效并显示计算机的价格 (=500)。如果更改它computer.cpu.price,则会报告相同的警告,但价格不再显示 - 它似乎不起作用。
问题是由于价格是一个虚拟属性,它可以工作!但是如果我在计算机组件内的另一个硬件组件上使用此属性,它就不起作用! Mido 发布的代码/答案让我希望有一个解决方案,它看起来非常接近!我希望我能完成这项工作。
【问题讨论】:
-
“它不起作用”是什么意思?您收到哪个错误?
-
对不起,像这样的问题很糟糕。正如@skypjack 已经提到的,“它不起作用”是一个典型的、懒惰的短语,总是会被否决。 qCring 提出了一个不可避免的问题,即“您的其他代码在哪里我们无法回答这个问题”。你在这里已经四年了……我只是不明白。
-
这不是真的。如果您有一个最小的示例(几乎总是可以创建),那么 main.cpp 会非常小——它们通常用于 Qt Quick 应用程序。
-
我看过代码,你有3个错误:1.
virtual Q_INVOKABLE void add(HardwareComponent * item);应该是Q_INVOKABLE virtual void add(HardwareComponent * item);2.Q_PROPERTY( HardwareComponent * cpu READ getCPU );去掉;在行尾。 3.engine.rootContext()->setContextProperty("computer", computer);应该放在engine.load(QUrl(QStringLiteral("qrc:/main.qml")));之前,这将解决您的警告问题。对于根本问题,您应该阅读如何在 qt doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html 中进行分组属性 -
根据答案修复问题中的代码是非常糟糕的形式,因为它使问题似乎不再有意义。我已恢复您的编辑。