C++ and QML integration, can't seem to access properties
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;
};
开发者_JS百科
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!
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++:
QDeclarativeView view;
CategoryPane pane;
view.rootContext()->setContextProperty("categoryPane", &pane);
QML:
import QtQuick 1.0
Rectangle {
width: 300
height: 300
Text {
text: categoryPane.catName
}
}
精彩评论