Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. ListElement: cannot use script for property value error [Qt Quick/C++]

ListElement: cannot use script for property value error [Qt Quick/C++]

Scheduled Pinned Locked Moved QML and Qt Quick
7 Posts 4 Posters 11.1k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kingsta
    wrote on last edited by
    #1

    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.1

    import SteelProfile 1.0

    Item {
    width: parent.width
    height: parent.height

    SteelProfile{
        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();
    

    }
    @

    1 Reply Last reply
    0
    • O Offline
      O Offline
      onek24
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kingsta
        wrote on last edited by
        #3

        [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(&#41; const;
        void setSteelStandardName(const QString &steelStandardName);
        

        ...@

        1 Reply Last reply
        0
        • O Offline
          O Offline
          onek24
          wrote on last edited by
          #4

          Alright, i'll check it out tomorrow.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            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 QML

            Hope it helps

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kingsta
              wrote on last edited by
              #6

              [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 QML

              Hope 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?

              1 Reply Last reply
              0
              • R Offline
                R Offline
                Rolias
                wrote on last edited by
                #7

                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.

                Check out my third course in the trilogy on Qt
                "Integrating Qt Quick with C++"
                http://bit.ly/qtquickcpp
                published by Pluralsight

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved