Results 1 to 3 of 3

Thread: Exchange data between “pure” C++ and QML

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2013
    Posts
    9
    Thanked 1 Time in 1 Post

    Default Exchange data between “pure” C++ and QML

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Exchange data between “pure” C++ and QML

    I think you should implement the observer/listener pattern between your framework-independent part and Qt wrapper for it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Exchange data between “pure” C++ and QML

    Quote Originally Posted by MattieB View Post
    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
    Qt Code:
    1. class WorkflowQMLInterface : public QObject
    2. {
    3. Q_OBJECT
    4. Q_PROPERTY(QStringList stepNames READ stepNames CONSTANT)
    5. Q_PROPERTY(QString currentStep READ currentStep NOTIFY currentStepChanged);
    6.  
    7. public:
    8. QStringList stepNames() const; // return names of workflow steps
    9.  
    10. QString currentStep() const; // return name of current step, or change to int and use as index in stepNames
    11.  
    12. public slots:
    13. void nextStep(); // advances the workflow, can also be a Q_INVOKABLE if you prefer
    14.  
    15. signals:
    16. void currentStepChanged();
    17. };
    To copy to clipboard, switch view to plain text mode 

    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,
    _

Similar Threads

  1. Dynamic Data Exchange (DDE) Server in Qt Application
    By tonka3000 in forum Qt-based Software
    Replies: 2
    Last Post: 2nd March 2013, 06:43
  2. Dynamic Data Exchange (DDE)
    By mlheese in forum Qt Programming
    Replies: 1
    Last Post: 11th November 2011, 02:57
  3. Server data exchange strategy
    By thru in forum General Programming
    Replies: 3
    Last Post: 21st July 2010, 20:15
  4. QTableWidget row-column exchange
    By SnarlCat in forum Qt Programming
    Replies: 0
    Last Post: 28th August 2008, 13:49

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.