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

Acess an element of QList<QObject*> that we exposed to QML



  • Hello,
    If i expose a list of objects in QML, is there anyway to acess an element of my list in QML. For example,
    i expose my list of object like this to my QML:

        mEngine.rootContext()->setContextProperty("plans",QVariant::fromValue(manager->getPlanningList()));
    
    

    I i try to make that call in QML

    plans[0].setManipTimeStartFromDB()
    

    It returns me the error : "Cannot call method 'setManipTimeStartFromDB' of undefined"


  • Moderators

    @Babs said in Acess an element of QList<QObject*> that we exposed to QML:

    setManipTimeStartFromDB

    did you mark it as Q_INVOKABLE ?



  • @J-Hilk yes i did


  • Moderators

    @Babs you don't give much information some code would be helpful

    you said, list of objects, QObject? (as base class)
    did you add the Q_OBJECT macro ?



  • @J-Hilk i did that also.
    So here is my class Planning

    class Planning: public QObject
    {
        Q_OBJECT
        Q_PROPERTY(int doseParameter READ doseParameter WRITE setDoseParameter NOTIFY doseParameterChanged)
        Q_PROPERTY(int numOfSteps READ numOfSteps WRITE setNumOfSteps NOTIFY numOfStepsChanged)
        Q_PROPERTY(double distance READ distance WRITE setDistance NOTIFY distanceChanged)
        Q_PROPERTY(int timeBetweenSteps READ timeBetweenSteps WRITE setTimeBetweenSteps NOTIFY timeBetweenStepsChanged)
        Q_PROPERTY(QString startManipTime READ startManipTime WRITE setStartManipTime  NOTIFY startManipTimeChanged)
        Q_PROPERTY(QTime startHour READ startHour WRITE setStartHour NOTIFY startHourChanged)
        Q_PROPERTY(QTime endHour READ endHour WRITE setEndHour NOTIFY endHourChanged)
        Q_PROPERTY(QTime tempsMesure READ tempsMesure WRITE setTempsMesure NOTIFY tempsMesureChanged)
        Q_PROPERTY(QString userName READ userName WRITE setUserName NOTIFY userNameChanged)
    
    public:
        explicit Planning(QObject *parent=Q_NULLPTR);
    
        int doseParameter() const;
    
        int numOfSteps() const;
    
        double distance() const;
    
        int timeBetweenSteps() const;
    
        QString startManipTime() const;
    
        QMap<int,Palier*> getPaliers();
    
        QTime startHour() const;
    
        QTime endHour() const;
    
        QTime tempsMesure() const;
    
        QString userName() const;
    
    public slots:
    
        void setDoseParameter(int doseParameter);
    
        void setNumOfSteps(int numOfSteps);
    
        void setDistance(double distance);
    
        void setTimeBetweenSteps(int timeBetweenSteps);
    
        void manageManipStartTime(QDateTime &time);
    
        Q_INVOKABLE void setManipTimeStartFromDB(void);
    
        void addPalier(Palier *pal);
    
        void addPalier(int iD, int value, int debitDose, double distance,qint64 iraTime, QString iraStart, QString iraEnd, QString mesTime);
    
        Q_INVOKABLE void defineStartDay(int hours,int minutes,int seconds);
    
        Q_INVOKABLE void defineEndDay(int hours,int minutes,int seconds);
    
        Q_INVOKABLE void defineMesureTime(int hours,int minutes,int seconds);
    
        bool isInTimeRange(QString startIra,QString endIra);
    
        qint64 maxPossibleSecondsToAdd(void);
    
        qint64 maxPossibleSecondsToSubstract(void);
    
        void shiftDate(QDateTime &endTime);
    
        void shiftTime(QDateTime &end);
    
        void manageFirstStep(QDateTime &startIradTime, QDateTime &endIradTime,qint64 &iraTime);
    
        void manageOtherSteps(QDateTime &startIradTime, QDateTime &endIradTime,qint64 &iraTime);
    
        Q_INVOKABLE void updatePlanning(void);
    
        Q_INVOKABLE void calculatePlanning(void);
    
        Q_INVOKABLE void addValue(double value,int index);
    
        int calculIradiationTime(double valuePalier);
    
        void setStartManipTime(QString startManipTime);
    
        void setStartHour(QTime startHour);
    
        void setEndHour(QTime endHour);
    
        Q_INVOKABLE void resetPaliers(void);
    
        bool isonWorkTime(QDateTime &dateTime);
    
        void updateAllPlanning();
    
        bool endIsWeekDay(QDateTime end);
    
        QDateTime calculateEndDate(QDateTime &startDateTime, qint64 duration);
    
        void setTempsMesure(QTime tempsMesure);
    
        Q_INVOKABLE void addToDatabase(void);
    
        void setUserName(QString userName);
    
    signals:
    
        void doseParameterChanged(int doseParameter);
    
        void numOfStepsChanged(int numOfSteps);
    
        void distanceChanged(double distance);
    
        void timeBetweenStepsChanged(int timeBetweenSteps);
    
        void startManipTimeChanged(QString startManipTime);
    
        void prePalierAdded();
    
        void postPalierAdded();
    
        void prePalierRemoved(int index);
    
        void postPalierRemoved();
    
        void startHourChanged(QTime startHour);
    
        void endHourChanged(QTime endHour);
    
        void tempsMesureChanged(QTime tempsMesure);
    
        void userNameChanged(QString userName);
    
    private:
        QDateTime m_start;
    
        QDateTime m_end;
    
        int m_doseParameter;
    
        int m_numOfSteps;
    
        double m_distance;
    
        int m_timeBetweenSteps;
    
        QString m_startManipTime;
    
        QMap<int,Palier*> m_paliers;
    
        QMap<int,int> m_valeursPaliers;
    
        QTime m_startHour;
    
        QTime m_endHour;
    
        QString m_startTime;
    
        QTime m_tempsMesure;
    
        QString m_userName;
    };
    
    #endif // PLANNING_H
    

    I expose a list of Planning to my QML.


  • Moderators

    @Babs well, are you sure, the list is not empty when you try to access it?

    what does length say ?



  • @J-Hilk thanks you. The list was empty.


Log in to reply