Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Is it possible to invoke C++ function before an item draw by qml?
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 286 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • AlienA Offline
    AlienA Offline
    Alien
    wrote on last edited by Alien
    #1

    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

    1 Reply Last reply
    0
    • rrlopezR Offline
      rrlopezR Offline
      rrlopez
      wrote on last edited by rrlopez
      #2

      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.

      Lic-Ing. Rodrigo Lopez Gonzalez
      Embedded Software Engineer
      RidgeRun Engineering Ltd.
      www.ridgerun.com
      Email: rodrigo.lopez@ridgerun.com

      AlienA 1 Reply Last reply
      1
      • rrlopezR rrlopez

        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.

        AlienA Offline
        AlienA Offline
        Alien
        wrote on last edited by
        #3

        Dear @rrlopez Ok I see thanks

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved