C++和 QML 集成,似乎无法访问属性
好的。所以我以为我已经完全理解了这一点,但显然我做错了一些事情,而且我一生都无法理解什么。我已严格按照教程进行操作(我检查过),但无法使其正常工作,所以在这里,让我问一个简单的问题。
我创建了一个基于 QObject 的类,它有一个简单的 QString 变量,用于存储类的名称(这仅用于测试),它看起来像这样:
#include <QObject>
class CategoryPane : public QObject
{
Q_OBJECT
Q_PROPERTY(QString catName READ getCategoryName WRITE setCategoryName);
public:
explicit CategoryPane(QObject *parent = 0);
QString getCategoryName();
void setCategoryName(QString);
signals:
void nameChange();
private:
QString categoryName;
};
这是使用以下函数注册的: qmlRegisterType("ITI_UI", 1, 0, "类别窗格");
我试图在 QML 文件中打印出我的 CategoryPane 类的名称变量,如下所示:
import QtQuick 1.0
import ITI_UI 1.0
Rectangle {
width: 300
height: 300
CategoryPane {
id: whatever
catName: "ey"
Text {
text: whatever.catName
}
}
}
但出现以下错误: qrc:/main.qml:11:3: Cannot allocate to non-existent default属性
注意:如果删除 Text {} 字段,我不会收到错误消息,但我又无法打印出我的姓名字符串,这就是重点...
提前感谢您的时间和耐心!
Ok. So I thought i had understood this completely, but obiously I have done something wrong and I can't for the life of me understand what. I've followed the tutorials to the letter (i checked) but can't get it to work, so here, let me ask a simple question.
I've created a QObject based class which has a simple QString variable that stores the name of the class (this is just for testing), it looks like this:
#include <QObject>
class CategoryPane : public QObject
{
Q_OBJECT
Q_PROPERTY(QString catName READ getCategoryName WRITE setCategoryName);
public:
explicit CategoryPane(QObject *parent = 0);
QString getCategoryName();
void setCategoryName(QString);
signals:
void nameChange();
private:
QString categoryName;
};
This is registered with the following function: qmlRegisterType("ITI_UI", 1, 0, "CategoryPane");
And I'm trying to print out the name variable of my CategoryPane class in a QML-file that looks like this:
import QtQuick 1.0
import ITI_UI 1.0
Rectangle {
width: 300
height: 300
CategoryPane {
id: whatever
catName: "ey"
Text {
text: whatever.catName
}
}
}
But I get the following error: qrc:/main.qml:11:3: Cannot assign to non-existent default property
Note: I get no error msg if I remove the Text {} field, but then again I can't print out my name string which is the whole point...
Thanks in advance for your time and patience!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CategoryPane 像可视项一样使用,但它派生自 QObject。您是否尝试过从 QDeclarativeItem 继承?
如果您只想访问该属性而不是将其用作可视项,则应该能够执行以下操作:
C++:
QML:
CategoryPane is being used like a visual item, but it derives from QObject. Have you tried inheriting from QDeclarativeItem instead?
If you just want to access the property and not use it as a visual item, you should be able to do like:
C++:
QML: