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] One out of two Q_PROPERTY working
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] One out of two Q_PROPERTY working

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 1 Posters 729 Views 1 Watching
  • 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.
  • M Offline
    M Offline
    MaxL
    wrote on last edited by
    #1

    Hi everyone,

    I'm back with an other issue concerning Q_PROPERTY of one my object exposing another custom class that is not working properly.

    Here is some code to understand what is going on :

    -C++:
    @
    class My1stObject : public QObject
    {
    Q_OBJECT
    Q_PROPERTY( QString name READ name() NOTIFY nameChanged )

    public:

    My1stObject( QObject* parent = nullPtr );
    
    QString name(){ return _name; }
    void setName( QString name ){ _name = name; emit( nameChanged() ); }
    

    signals:
    void nameChanged();
    private:
    QString _name;
    }
    @
    @
    class My2ndObject : public QObject
    {
    Q_OBJECT
    Q_PROPERTY( int index READ index() NOTIFY indexChanged )
    Q_PROPERTY( My1stObject* currentObject READ getCurrentObject NOTIFY currentObjectChanged )
    public:

    My2ndObject( QObject* parent = nullPtr );
    
    int index(){ return _currentIndex; }
    void setCurrentIndex( int index ){ _currentIndex = index; emit( indexChanged() ); }
    My1stObject* getCurrentObject(){ return _currentObject ; }
    void setCurrentObject( My1stObject* obj ){ _currentObject = obj; emit( currentObjectChanged ) ); }
    

    signals:
    void indexChanged();
    void currentObjectChanged();
    private:
    int _currentIndex;
    My1stObject* _currentObject;
    }
    @

    Main:
    @
    QGuiApplication app( argc, argv );

    qmlRegisterType< My1stObject >( "com.example", 1,0, "My1stObject" );
    qmlRegisterType< My2ndObject >( "com.example", 1,0, "My2ndObject" );

    My1stObject object1;
    My2ndObject object2;

    object1.setName( "MyTestObject" )
    object2.setCurrentObject( &object1 );
    object2.setCurrentIndex( 1 );

    QQuickView myView;
    myView.rootContext()->setContextProperty( "my2ndobject", &object2 );
    myView.setSource( "qrc:/qml/Main.qml" );

    myView.showFullScreen();
    return app.exec();
    @

    Main.qml:

    @
    import com.example 1.0
    Column
    {
    Text
    {
    property int currentIndex: my2ndobject.currentIndex
    property My1stObject object1: my2ndobject.currentObject
    text: currentIndex + " " + object1.name
    }
    Text
    {
    text: my2ndobject.currentIndex + " " +my2ndobject.currentObject.name
    }
    }
    @

    When running, it says that for the first Text, it cannot read property name of a null element while the second text perfectly show the object name. I have also tried with Binding but it does not change a thing. This behavior seems very strange to me because for currentIndex it works fine in both cases.

    Any idea ?
    Thanks

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MaxL
      wrote on last edited by
      #2

      I have been looking for this for about an hour and four my problem, silly mistakes when registering types with copy/paste , both My1stObject and My2ndObject where registered with the same qmlName

      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