PDA

View Full Version : Exchange data between “pure” C++ and QML



MattieB
19th September 2013, 08:45
Hi

I’m making a small test application which has a QML UI and some “pure” c++ logic (by which i mean it doesn’t depend on the Qt framework). On my model side, I have a Workflow class which has a vector of Workflow Steps. Each Worfklow step has a name. The workflow also has a currently “active” Workflow Step My UI has a button which cycles through the workflow steps (basically, it instructs the workflow to make the next Workflow Step in it’s vector the active Workflow Step).
I’ve made a class, WorkflowQMLInterface (Q_OBJECT) which has a pointer to my pure c++ Workflow class and has some Q_INVOKABLE methods (asking the name of Workflow Step with index i, check if a Workflow Step with index i is the active workflow,… all using the Workflow member of this class to get the correct values). My QML UI uses this interface (.rootContext()->setContextProperty(“workflowQML”, WorkflowQMLInterface )) to query the model for the data. So far, so good. now my UI lists all the workflow steps with the active step being printed in bold. I also have a button in my UI which instructs the workflow to go to the next step (by invoking a method of WorkflowQMLInterface which updates the model). The model gets updated when I press this button, but my UI doesn’t follow (the next workflow step doesn’t become bold). What is the best way to make the UI follow the model (is it even possible)? Or do you have another way of working to keep your logic independent from the UI framework?

Matt

Ps: I know that I could rewrite my model to use Q_PROPERTIES, but I want it to be as framework independent as possible.

wysota
19th September 2013, 09:54
I think you should implement the observer/listener pattern between your framework-independent part and Qt wrapper for it.

anda_skoa
19th September 2013, 12:58
Ps: I know that I could rewrite my model to use Q_PROPERTIES, but I want it to be as framework independent as possible.

You don't need to use Qt properties in your model but it would be a good idea for the WorkflowQMLInterface class.

Something like this


class WorkflowQMLInterface : public QObject
{
Q_OBJECT
Q_PROPERTY(QStringList stepNames READ stepNames CONSTANT)
Q_PROPERTY(QString currentStep READ currentStep NOTIFY currentStepChanged);

public:
QStringList stepNames() const; // return names of workflow steps

QString currentStep() const; // return name of current step, or change to int and use as index in stepNames

public slots:
void nextStep(); // advances the workflow, can also be a Q_INVOKABLE if you prefer

signals:
void currentStepChanged();
};


In nextStep(), when you have change the current workflow has been changed by your model, emit currentStepChanged() so that the QML can react to that.

Cheers,
_