Nominate our 2022 Qt Champions!

Importing canvas data to a C++ backend

  • I'm designing something in QML that needs to draw something based on people's inputs to draw something on a canvas. The inputs have to go through an equation in order to get the info for what needs to be drawn on the screen. This equation also needs x and y coordinates in order to come up with values. Is there any way where I am able to send each individual x and y coordinate data to a C++ backend in order to enter it into an equation?


  • @GoldRatio hi,
    if you want to call c++ method with parameters from your QML, yes u can.

    class BackEnd : public QObject
    Q_INVOKABLE void fnc(float x,float y){
        explicit BackEnd(QObject *parent = nullptr);

    in your main.cpp include, and create an instance of BackEnd.
    Put that instance as ContextProperty to be able to call its methods (Q_INVOKABLES and public slots ) from QML

    BackEnd be;

    use from qml

    Component.onCompleted: { back.fnc(10,11) }

  • @LeLev Sorry I may have been a little vague. My problem is not that I am unable to connect import a function to the Backend, but that I don't know how to import the data from each individual point on a canvas into the backend.

    Example - Say if I create a QML canvas and I need to do something different with each coordinate, depending on the x and y values of its coordinates are (ie. sending the values into an equation to be evaluated). I don't know how to pull out these values and send them into the backend as variables to each be evaluated, and then have that information sent back to the canvas and interacted based on these values.

    Sorry for all of the trouble, I'm pretty new this stuff.

    Thanks Again!

  • Not sure again this is what you need but all properties qml side have onPropertieChanged signal
    peroperty real x
    onXChanged : {back.fnc(x,y)}

  • Qt Champions 2018

    a QML canvas and I need to do something different with each coordinate

    What does the coordinate of a canvas mean?

    How is the canvas painted ?
    If it's from user input, send the user input to your function first and the drawing part should then be driven by your backend, with a model, signals, whatever.

Log in to reply