[SOLVED]QML basic type problem



  • 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


  • Moderators

    Hi,

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



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



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



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



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



  • [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?



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



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.