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>
#endifclass QcppClock : public QObject
{
Q_OBJECTQ_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;
#endifpublic:
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();
}
@And the QML file using the exposed properties is:
@
import QtQuick 2.2
import QtQuick.Window 2.1Window {
visible: true
width: 800
height: 480MouseArea { 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 49The 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
-
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.