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. [SOLVED]QML basic type problem
QtWS25 Last Chance

[SOLVED]QML basic type problem

Scheduled Pinned Locked Moved QML and Qt Quick
10 Posts 4 Posters 2.3k 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.
  • V Offline
    V Offline
    Vardan95
    wrote on last edited by
    #1

    Hello,I have a problem with passing types between c++ and qml
    qml recongizes only QString type,but does't recognize bool,double
    @class ModelItem:public QObject{

    Q_OBJECT
    Q_PROPERTY(bool editable READ IsEditable WRITE setEditable NOTIFY editablechanged )
    Q_PROPERTY(double coefficient READ Coefficient WRITE setCoefficient NOTIFY coefficientchanged)
    Q_PROPERTY(double free_member READ Free_member WRITE setFree_member NOTIFY free_memberchanged)
    Q_PROPERTY(double degree READ Degree WRITE setDegree NOTIFY degreechanged)
    Q_PROPERTY(QString First READ First WRITE setFirst NOTIFY firstchanged)
    Q_PROPERTY(QString Second READ Second WRITE setSecond NOTIFY secondchanged)
    

    signals:
    void editablechanged();
    void coefficientchanged();
    void free_memberchanged();
    void degreechanged();
    void firstchanged();
    void secondchanged();

    public:

    ModelItem(QObject* parent=0):QObject(parent)
    {
    
    }
    
    bool setItem( Element& obj)
    {
        /............
    }
    
    
    bool IsEditable(){return editable;}
    void setEditable(bool obj){editable=obj; emit editablechanged();}
    
    double Coefficient(){return coefficient;}
    void setCoefficient(double  mycoefficient){coefficient=mycoefficient;emit coefficientchanged();}
    
    double Free_member(){.......}
    void setFree_member(double  myfree_member){.......;}
    
    QString First(){return first;}
    void setFirst(QString const str){first = str;emit firstchanged();}
    
    QString Second(){...........}
    void setSecond(QString const str){........}
    

    private:
    QString first;
    QString second;

    double  coefficient;
    double free_member;
    bool editable;
    

    };@
    Please help me to solve this problem :)

    Problem was here
    Q_OBJECT
    Q_PROPERTY(bool Editable READ IsEditable WRITE setEditable NOTIFY editablechanged )
    Q_PROPERTY(double Coefficient READ Coefficient WRITE setCoefficient NOTIFY coefficientchanged)
    Q_PROPERTY(double Free_member READ Free_member WRITE setFree_member NOTIFY

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      What do you mean by "doesn't recognize" ?
      From your code I don't see that you have passed arguments in signal.

      157

      1 Reply Last reply
      0
      • V Offline
        V Offline
        Vardan95
        wrote on last edited by
        #3

        [quote author="p3c0" date="1415337534"]Hi,

        What do you mean by "doesn't recognize" ?
        From your code I don't see that you have passed arguments in signal.[/quote]

        for example
        in delegate I have wrote modelData.Coefficient and it didn't work QML debugger said that "modelData.Coefficent is undefined"
        but when I try this modelData.First it works perfectly

        1 Reply Last reply
        0
        • M Offline
          M Offline
          morte
          wrote on last edited by
          #4

          "modelData.Coefficent" is not "modelData.coefficent"

          1 Reply Last reply
          0
          • V Offline
            V Offline
            Vardan95
            wrote on last edited by
            #5

            [quote author="morte" date="1415378497"]"modelData.Coefficent" is not "modelData.coefficent"[/quote]
            what? Can you explain please?

            1 Reply Last reply
            0
            • M Offline
              M Offline
              morte
              wrote on last edited by
              #6

              I mean there is property named "coefficient" and there is no property named "Coefficient"

              1 Reply Last reply
              0
              • V Offline
                V Offline
                Vardan95
                wrote on last edited by
                #7

                [quote author="morte" date="1415379542"]I mean there is property named "coefficient" and there is no property named "Coefficient"[/quote]

                In that case how works modelData.First or modelData.Second?

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  morte
                  wrote on last edited by
                  #8

                  Those properties named "First" and "Second" starting with first capital letter, and you access them from QML as "First" and "Second", looks like you have a typo at coefficient.

                  1 Reply Last reply
                  0
                  • V Offline
                    V Offline
                    Vardan95
                    wrote on last edited by
                    #9

                    Thank you very much,it seems that I haven't properly understood the meaning of Q_PropertY

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

                      Vardan95 - You might find my "blog post on an AUTO_PROPERTY":http://bit.ly/autoprop macro helpful. It describes a macro that lets you just type
                      @AUTO_PROPERTY(double, Coefficient)@

                      No need to write the getter, setter, or create the member variable. Originally I wrote it to just reduce clutter for trivial properties, but after reading your post I also realize it eliminates the possibility of making a typo error when creating a Q_PROPERTY and vastly reduces the chances of confusing which name to use in your QML.

                      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