0

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?

blessedone
  • 160
  • 1
  • 8
  • Aren't you starting two `QmlEngine`'s by having this `startGui` function? Can you also show the main function? – Amfasis Oct 02 '19 at 13:53
  • 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 have – blessedone Oct 02 '19 at 13:58
  • How you exposing class/property to QML ? where is `main()` in your code ? – Mohammad Kanan Oct 02 '19 at 15:08
  • @Gebre If you want help, provide an [MRE], otherwise your question is off-topic and is probably closed. – eyllanesc Oct 02 '19 at 15:15
  • 1
    @Gebre I think you shouldn't ask basically the same question twice. Please close this one as the other one is more "MRE" – Amfasis Oct 02 '19 at 18:29
  • How do I close it? – blessedone Oct 02 '19 at 18:33
  • below the tag list there should be a `delete` option (As well as `share`, `edit`, `flag` and `close`, the latter being the wrong one, since you will lock it and it still be visible) – Amfasis Oct 02 '19 at 18:54
  • @Amfasis , while asked on the same context and same code, each question is about different topic. the other question relates to the derived class signal. while this one is about how to update Q_PROPERTY of base class .. do you agree ? – Mohammad Kanan Oct 02 '19 at 21:00
  • I don't agree, the same solution as on the other question can be given on this question. However, here Gebre forgot to add the qml code, which displayed the misunderstanding (since signals can just simply be emitted from derived classes) and Gebre explained his use-case better in the other question – Amfasis Oct 03 '19 at 06:45
  • 1
    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. – blessedone Oct 03 '19 at 18:12

1 Answers1

1

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
}
Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47
  • Thanks Mohammad, I realised that and changed the m_seats variable to static. It now works when I print gps.seats in qml. However the signal updateSeats() does not seem to update the qml variable when it is emitted. How can I go about making that static or other solution? – blessedone Oct 02 '19 at 15:26
  • My observation is you emitting bare signal .. (it does not carry the updated value) .. I can't grant as sure this is the problem , but surely , you need this post : [Q_PROPERTY NOTIFY signal and its argument](https://stackoverflow.com/questions/43705363/q-property-notify-signal-and-its-argument) – Mohammad Kanan Oct 02 '19 at 15:29