Quantcast
Channel: Qt DevNet forums: Qt Quick 1283365070**
Viewing all articles
Browse latest Browse all 4972

[SOLVED] One out of two Q_PROPERTY working

$
0
0
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

Viewing all articles
Browse latest Browse all 4972

Latest Images

Trending Articles



Latest Images