ListElement: cannot use script for property value error [Qt Quick/C++]
-
Hi guys
I want to use Qt Quick/C++ together. For this, I wrote SteelProfile Class (C++) and own functions. I can't assign "steelprofileID.steelStandardName" value to ListElement. There is a weird situation. This code(on below) shows to me correct value, but I can't assign to ListElement. I got ListElement: cannot use script for property value error.
@console.log("Value: " + steelprofileID.steelStandardName)@
There is the problem?
steelprofile.h
@class SteelProfile : public QObject
{
Q_OBJECT
Q_PROPERTY(QString steelStandardName READ steelStandardName WRITE setSteelStandardName) //NOTIFY steelStandardNameChanged
public:
explicit SteelProfile(QObject *parent = 0);signals:
public slots:
Q_INVOKABLE QString steelStandardName() const;
void setSteelStandardName(const QString &steelStandardName);private:
QString sName;};@
steelprofile.cpp
@SteelProfile::SteelProfile(QObject *parent) :
QObject(parent)
{
qDebug() << "SteelProfile()";sName = "Europe";
}
QString SteelProfile::steelStandardName() const{
qDebug() << "steelStandardName()"; return sName;
}
void SteelProfile::setSteelStandardName(const QString &steelStandardName){
qDebug() << "setSteelStandardName()"; sName = steelStandardName;
}
@qml file
@import QtQuick 2.1
import QtQuick.Controls 1.1
import QtQuick.Controls.Styles 1.1import SteelProfile 1.0
Item {
width: parent.width
height: parent.heightSteelProfile{ id: steelprofileID steelStandardName: "Europe" } ListModel{ id:steelStandardModel ListElement{ //steelStandardName: "Europe" steelStandardName: steelprofileID.steelStandardName //Gets error here ... } ListElement{ steelStandardName: "US" ... } }
...
@main.cpp
@#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"
#include "steelprofile.h"#include <QQmlApplicationEngine>
#include <QQuickWindow>
#include <QQmlComponent>int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);qmlRegisterType<SteelProfile>("SteelProfile", 1,0, "SteelProfile"); SteelProfile *sProfile = new SteelProfile; QQmlApplicationEngine engine; engine.load(QUrl("qml/StackView1/main.qml")); QObject *topLevel = engine.rootObjects().value(0); QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel); window->show(); return app.exec();
}
@ -
Hello, try to put your
@Q_INVOKABLE QString steelStandardName() const;@
as
@QString steelStandardName() const;@
in public and not in public slot. Probably this will fix it, but im not absolutely sure. For further information please read:
"Defining New QML Elements":http://qt-project.org/doc/qt-4.8/qtbinding.html#defining-new-qml-elements -
[quote author="onek24" date="1391100071"]Hello, try to put your
@Q_INVOKABLE QString steelStandardName() const;@
as
@QString steelStandardName() const;@
in public and not in public slot. Probably this will fix it, but im not absolutely sure. For further information please read:
"Defining New QML Elements":http://qt-project.org/doc/qt-4.8/qtbinding.html#defining-new-qml-elements
[/quote]Firstly, thanks for relpy.
I changed my header file and I got the error again
header file
@...
public:
explicit SteelProfile(QObject *parent = 0);QString steelStandardName() const; void setSteelStandardName(const QString &steelStandardName);
...@
-
Hi,
You should take a look at the example in:
qt5_intall_folder/qtquick1/examples/declarative/modelviews/objectlistmodel/ you have an example of an object implemented in C++ that you can then use in QMLHope it helps
-
[quote author="SGaist" date="1391113099"]Hi,
You should take a look at the example in:
qt5_intall_folder/qtquick1/examples/declarative/modelviews/objectlistmodel/ you have an example of an object implemented in C++ that you can then use in QMLHope it helps[/quote]
Firstly, Im sorry for the late my answer. I've been away.
In this example, @Text { text: name }@ code perfectly runs. I can't understand @color: model.modelData.color@ code. What is the model and modelData? Where did they declare?
I use the same figure as that example. But I can't fix the issue.
@steelStandardName: steelprofileID.steelStandardName@
or
@steelStandardName: SteelProfile.steelStandardName@
This two codes didn't run.I write some explanations to easly understand code.
steelStandardName: ListElement object
steelprofileID: SteelProfile object in qml
steelStandardName: A function in cpp file and returns QString name of steel standard.What should I do my friends?
-
Well even though this is ten months old I'll post an answer to this latest question for any future visitors. model is a property on the ListView that lets you walk up the chain to the model from the delegate. The modelData property holds the QObject*. This makes the properties of the object directly accessible in the ListView delegate using the syntax shown that is:
@model.modelData.color@is returning the Q_PROPERTY color property from the DataObject instance.