Important: Please read the Qt Code of Conduct -

Q_PROPERTY not working properly on linux distros

  • Hello all, i'm developing a clock application and I need to do it using Q_PROPERTY. The idea is to make all the clock control logic using C++ and deploy it in a QML GUI.
    I got this working on a windows machine but when I run it on a linux distro in a development board I get sometimes undefined text (which are the exposed properties from C++).

    The header file is:

    @#ifndef QCPPCLOCK_H
    #define QCPPCLOCK_H

    #include <QObject>
    #include <QDebug>
    #include <QString>
    #include <QTime>
    #ifdef linux
    #include <stdio.h>
    #include <sys/time.h>
    #include <time.h>
    #include <unistd.h>

    class QcppClock : public QObject

    Q_PROPERTY(QString hours READ hours WRITE setHours NOTIFY hoursChanged)
    Q_PROPERTY(QString minutes READ minutes WRITE setMinutes NOTIFY minutesChanged)

    QString actualHours;
    QString actualMinutes;
    QObject* rootObject;
    QTime actualTime;
    qint8 alarmHour;
    qint8 alarmMinutes;
    QString timeFormat;
    bool alarmSet;
    #ifdef linux
    struct tm* ptm;
    struct timeval tv;

    explicit QcppClock(QObject *parent = 0);
    QString hours();
    void setHours(QString);
    QString minutes();
    void setMinutes(QString);

    public slots:
    void qcppClock_vUpdateTextTime_slot(void);

    void qcppClock_vTriggerAlarm_signal(void);
    void hoursChanged(void);
    void minutesChanged(void);

    #endif // QCPPCLOCK_H@

    The .cpp file:

    @#include "qcppclock.h"
    #include <QTimer>
    #include <QtCore/qmath.h>

    QcppClock::QcppClock(QObject parent) :
    timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(qcppClock_vUpdateTextTime_slot()));
    rootObject = parent;
    actualTime = QTime::currentTime();
    /* Initialize the format to 24 hours */
    timeFormat = "24hours";
    #ifdef linux
    gettimeofday (&tv, NULL);
    ptm = localtime (&tv.tv_sec);
    int hour = ptm->tm_hour;
    //actualHours = QString::number(hour);
    int minutes = ptm->tm_min;
    //actualMinutes = QString::number(minutes);
    int seconds = ptm->tm_sec;
    (void)actualTime.setHMS(hour , minutes, seconds);


    QString QcppClock::hours()
    return actualHours;

    void QcppClock::setHours(QString newHours)
    actualHours = newHours;
    #ifdef linux
    tv.tv_sec += 3600 * actualHours.toInt();
    settimeofday(&tv, NULL);
    (void)actualTime.setHMS(actualHours.toInt() , actualMinutes.toInt(), actualTime.second());
    emit hoursChanged();

    QString QcppClock::minutes()
    return actualMinutes;

    void QcppClock::setMinutes(QString newMinutes)
    actualMinutes = newMinutes;
    #ifdef linux
    tv.tv_sec += 60 * actualMinutes.toInt();
    settimeofday(&tv, NULL);
    (void)actualTime.setHMS(actualHours.toInt() , actualMinutes.toInt(), actualTime.second());
    emit minutesChanged();

    void QcppClock::qcppClock_vUpdateTextTime_slot(void)
    actualTime = actualTime.addSecs(1);
    actualMinutes = QString::number(actualTime.minute());
    emit minutesChanged();
    actualHours = QString::number(actualTime.hour());
    emit hoursChanged();

    The main file, which instantiates the clock object is:

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    #include "qcpp_Clock/qcppclock.h"

    int main(int argc, char *argv[])
    QGuiApplication app(argc, argv);

    QQmlApplicationEngine engine;
    QcppClock qcpp_Clock;
    QQmlContext* context = engine.rootContext();
    context->setContextProperty("clockCtrl", &qcpp_Clock);
    return app.exec&#40;&#41;;


    And the QML file using the exposed properties is:

    import QtQuick 2.2
    import QtQuick.Window 2.1

    Window {
    visible: true
    width: 800
    height: 480

    MouseArea {
        anchors.fill: parent
        onClicked: {
    Text {
        property string prHours: clockCtrl.hours
        property string prMinutes: clockCtrl.minutes
        text: {
            console.log(clockCtrl, clockCtrl.hours, clockCtrl.minutes);
            return (prHours + " : " + prMinutes)
        anchors.centerIn: parent


    As you can see it's a short and easy code, the problem is in QML when I try to make reference to the C++ exposed properties, i added a couple of consoles in order to see the result in the log and It works like a charm in windows, I never get these "undefined text" but in the development board with linux distro I get sometimes:

    qml: QcppClock(0x7e8eeca8) 1 49
    qml: QcppClock(0x7e8eeca8) undefined undefined
    qml: QcppClock(0x7e8eeca8) 1 49

    The first debug is the address of the c++ exposed object in C++, then, the Q_PROPERTY "hour" and the Q_PROPERTY "minutes".

    Thank you in advance.


  • Maybe a silly comment, but have you tried to run qmake first? I get similar error (sometimes) when I do not run qmake before compilation (sometimes even a clean project is necessary).

  • @lynic thanks, I run qmake to create the specific Makefile, then i execute make and get the executable file.

Log in to reply