Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Is it possible to invoke C++ function before an item draw by qml?



  • Hello,

    I'm looking for a way to invoke my c++ function before qml item draw on the screen. for example imagine that you have a rectangle in qml which x and y properties calculated in C++ function with one parameter like below:

    C++ Header:

    #ifndef CTEST_H
    #define CTEST_H
    
    #include <QObject>
    
    class CTest : public QObject
    {
        Q_OBJECT
        Q_PROPERTY(int C_X READ getX WRITE setX NOTIFY xChanged)
        Q_PROPERTY(int C_Y READ getY WRITE setY NOTIFY yChanged)
    private:
        int x;
        int y;
    public:
        explicit CTest(QObject *parent = nullptr);
    
        int getX() const;
        void setX(int value);
    
        int getY() const;
        void setY(int value);
    
    signals:
           void xChanged();
           void yChanged();
    public slots:
        void someCalculation(int data);
    };
    
    #endif // CTEST_H
    
    

    C++ Source:

    #include "ctest.h"
    #include <QtMath>
    
    int CTest::getX() const
    {
        return x;
    }
    
    void CTest::setX(int value)
    {
        if(x == value)
            return;
        x=value;
        emit xChanged();
    }
    
    int CTest::getY() const
    {
        return y;
    }
    
    void CTest::setY(int value)
    {
        if(y == value)
            return;
        y=value;
        emit yChanged();
    }
    
    void CTest::someCalculation(int data)
    {
        int temp = static_cast<int>(pow(data,3));
        setX(temp/10);
        setY(temp/5);
    }
    
    CTest::CTest(QObject *parent) : QObject(parent)
    {
    
    }
    
    

    Qml doc:

    import QtQuick 2.12
    import QtQuick.Window 2.12
    import io.test 1.0
    Window
    {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        CTest
        {
            id:ctestID
        }
    
        Rectangle
        {
            id:rectID
            color: "purple"
            //Where should I invoke CTest.someCalculation(7) 
            x:ctestID.C_X
            y:ctestID.C_Y
            width: 100
            height: 100
        }
    }
    

    It seems that C++ function can invoke on qml event hadler like onClicked or something similar but here I don't have any event handler (Also Component and onCompleted is not adequate to this example because I need calculation before drwing the rectangle).

    Best Regards,
    Alien



  • You can do it on the constructor of your class, if you want to have a value fixed (like the 7 on your example). This way, the calculation is done when the ctestID class is created and hence, the item will have proper dimensions.
    Edit: If you don't want to do it on the constructor, you can always implement an activate public function to be called whenever you want.



  • Dear @rrlopez Ok I see thanks


Log in to reply