Search code examples
c++qtqmlqt-quick

QML newbie needing some help in hooking up C++ backend with QML frontend


I have code in C++ for saving and retrieving data from an XML file. I have some forms built in QML that I would like to hook up in such a way that when data is entered in my QML, processing is handled in C++, and searches are done for products via QML form, processed in C++ and list of product items are handed back to QML for display.

class ProcessRequests : public QObject
{
    Q_OBJECT
    Q_PROPERTY(string username READ username WRITE username)
    Q_PROPERTY(string useremail READ useremail WRITE useremail)
    Q_PROPERTY(string usercomplaint READ usercomplaint WRITE usercomplaint)


    public:

    ProcessRequests()
    {}
    ~ProcessRequests(){}

    Q_INVOKABLE void SubmitComplaint(){
     //TODO: Add Xml code to save the property values to file 

    }

};

Solution

  • I think your problem is to inter-communicate between QML and C++ code, you can do it with code like this:

    //Product.cpp
    QmlApplicationViewer viewer;
    
    QDeclarativeEngine *engine = viewer.engine();
    QDeclarativeContext *context = engine->rootContext();
    
    context->setContextProperty("Product", this);
    
    //Your QML File
    Product.YourFunction(args);