0
votes

I basically just want to use multiple derived classes to change a member variable of a base class and to forward that value to qml using qproperty, but for some reason it's not working

car.h

    #include <QObject>

    class Car : public QObject{
        Q_OBJECT
        Q_PROPERTY(int seats MEMBER m_seats NOTIFY updateSeats)

    public:
        explicit Car(QObject *parent = 0);
        ~Car();
        int m_seats;
        Q_INVOKABLE void test();

    signals:
        void updateSeats();
    };

car.cpp

    #include "car.h"

    Car::Car(QObject *parent) :
        QObject(parent),
        m_seats(0)
    {

    }

    Car::test(){
        m_seats = 5;
        emit updateSeats();
    }

    Car::~Car(){}

toyota.h

    #include "car.h"

    class Toyota : public Car{
        Q_OBJECT


    public:
        explicit Toyota(QObject *parent = 0);
        ~Toyota();
        void foundCar();
    };

toyota.cpp

    #include "toyota.h"

    Toyota::Toyota(QObject *parent) 
    {

    }

    Toyota::foundCar(){
        m_seats = 4;
        emit updateSeats();
    }

    Toyota::~Toyota(){}

Now, after invoking the foundCar function in class Toyota, if I do console.log(car.seats) in qml I get 0, but I expect it to be 4 because I am modifying it in the derived class. However if I call car.test() from qml and then I print car.seats, the value is 5. I am confused why this is the case. In qml I want car.seats to be 4. What am I missing?

1
Aren't you starting two QmlEngine's by having this startGui function? Can you also show the main function?Amfasis
I have edited the question, basically starting the engine is taken care of in the main, that part has no problems as it works for all the other classes I haveGebre
How you exposing class/property to QML ? where is main() in your code ?Mohammad Kanan
@Gebre I think you shouldn't ask basically the same question twice. Please close this one as the other one is more "MRE"Amfasis
The questions are actually about two different things. This one I solved by making m_seats static. However I couldn't do the same for the signal, qt won't allow it. @Amfasis when I was emitting the signal from the derived class, it wasn't updating, because it was of object Toyota, not car.Gebre

1 Answers

1
votes

Toyota object is derived class object Toyota of base Car not an object of Car you are modifying derived class object member Toyota::m_seats and that won't have any effect on direct base Car object .. and because Q_PROPERTY is defined only in base class Car .. the only value QML would see is base class object .. and specifically the object you set in setContextProperty ... the code you omitted after the post edit.

From previous edits in your post, you seem to set the setContextProperty in your engine as Car object .. its in this object where you need to modify member that is a Q_PROPERTY.

I am referring to your code:

Car::startGui(){
    QQmlApplicationEngine engine;
    QQmlContext *ctxt = engine.rootContext();
    ctxt->setContextProperty("car", this)
    // start engine, which works properly
}