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. Q_PROPERTY not working properly on linux distros
Forum Updated to NodeBB v4.3 + New Features

Q_PROPERTY not working properly on linux distros

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 1.2k Views 1 Watching
  • 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.
  • R Offline
    R Offline
    ramsesrago
    wrote on last edited by
    #1

    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>
    #endif

    class QcppClock : public QObject
    {
    Q_OBJECT

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

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

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

    public slots:
    void qcppClock_vUpdateTextTime_slot(void);

    signals:
    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) :
    QObject(parent)
    {
    QTimer
    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";
    timer->start(1000);
    #ifdef linux
    gettimeofday (&tv, NULL);
    ptm = localtime (&tv.tv_sec);
    int hour = ptm->tm_hour;
    //setHours(hour);
    //actualHours = QString::number(hour);
    int minutes = ptm->tm_min;
    //setMinutes(minutes);
    //actualMinutes = QString::number(minutes);
    int seconds = ptm->tm_sec;
    (void)actualTime.setHMS(hour , minutes, seconds);
    #endif

    }

    QString QcppClock::hours()
    {
    return actualHours;
    }

    void QcppClock::setHours(QString newHours)
    {
    actualHours = newHours;
    #ifdef linux
    tv.tv_sec += 3600 * actualHours.toInt();
    settimeofday(&tv, NULL);
    #else
    (void)actualTime.setHMS(actualHours.toInt() , actualMinutes.toInt(), actualTime.second());
    #endif
    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);
    #else
    (void)actualTime.setHMS(actualHours.toInt() , actualMinutes.toInt(), actualTime.second());
    #endif
    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();
    QString::number(actualTime.minute());
    }
    @

    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);
    engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
    
    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: {
            Qt.quit();
        }
    }
    
    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.

    Ramsés

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lynic
      wrote on last edited by
      #2

      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).

      1 Reply Last reply
      0
      • R Offline
        R Offline
        ramsesrago
        wrote on last edited by
        #3

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

        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