Exposing C++ values to QML issue
-
Since you don't initialize your members I would guess "if (!dataObject.isEmpty())" is false so your values will not be set so you see the uninitialized data in your qml
-
Please properly initialize the members in the ctor and see if the 'strange' values disappear.
-
I initialized all members but I still didn't get any values displayed on the qml side ...
AlertMessage::AlertMessage(QObject *parent) : QObject(parent), m_messageType(""), m_sender(""), m_alertType(""), m_id(""), m_origin(""), m_dataSource(""), m_distanceToEvent(0), m_severity(0), m_azimuth(0), m_timeToEvent(0) { }
-
Here is a snippet of my QML code
import QtQuick 2.0 import io.qt.alertmessage 1.0 import "../BasicItems" import "../ModellingItems" import "../../WarningCalc.js" as WarningCalcer Rectangle { id: informationCellArea property int informationTextHeaderSize: 10 property int informationTextSize: 17 property int itemHeight: parent.height/4 property int headerToTextMargin: 10 property string senderString property string distanceString property string timeString property string descriptionString property string trafficSignString property int severityInt color: "transparent" radius: 10 AlertMessage { id: alertMessage } Item { id: distanceArea width: parent.width/2 height: itemHeight anchors.left: parent.left anchors.bottom: descriptionUseCaseArea.top HMIText { id: distanceTextHeader text: "Distance to event" fontSize: informationTextHeaderSize anchors.centerIn: parent } HMIText { id: distanceText text: alertMessage.distanceToEvent + " m" fontSize: informationTextSize useFontBold: true anchors.top: distanceTextHeader.bottom anchors.topMargin: headerToTextMargin anchors.horizontalCenter: parent.horizontalCenter onTextChanged: { console.log("QML:" + distanceString); } } } }
-
@Thanni123 said in Exposing C++ values to QML issue:
get any values displayed on the qml side ...
You don't see any double value or you don't see the strange values anymore? Please initialize one string to something other than an empty string.
-
@fcarney I've posted the method declaration above and the printout method is only a debug method to display if the values were set.
I think I did a mistake during initializing an object of my AlertMessage class but I didn't get my fault yet... It must be some stupid mistake I've done.
-
@Thanni123 said in Exposing C++ values to QML issue:
But if I try to set the values with the json file the value mentioned above appear or the value equals 0.
The problem is in readDataFromJSONFormat and/or printout. If you want help you need to show what is in those. So far nothing you have posted shows any problems.
-
Hey, I don't get your point exactly but here is the source code of your request for both methods including the output for the printout mehtod.
void AlertMessage::readDataFromJSONFormat(const QJsonObject &jsonObject) { if (jsonObject.count() <= 0) { qDebug() << "AlertMessage::Wrong JSON-Array."; return; } cleanMessage(); setMessageType(jsonObject["type"].toString()); setSender(jsonObject["sender"].toString()); QJsonValue valueData = jsonObject.value(QString("data")); QJsonObject dataObject = valueData.toObject(); if (!dataObject.isEmpty()) { setAlertType(dataObject["alert"].toString()); setID(dataObject["id"].toString()); setOrigin(dataObject["origin"].toString()); setDataSource(dataObject["data_source"].toString()); setDistanceToEvent(dataObject["distance_to_event"].toDouble()); setSeverity(dataObject["severity"].toDouble()); setAzimuth(dataObject["azimuth"].toDouble()); setTimeToEvent(dataObject["time_to_event"].toDouble()); QJsonValue detailValue = dataObject.value(QString("detail")); QJsonObject detailObject = detailValue.toObject(); if (!detailObject.isEmpty()) { setDetailLevel(detailObject["level"].toInt()); } } }
QString AlertMessage::printout() { QString print = "** AlertMessage begin**\n"; print += "MessageType = " + QString("%1").arg(this->messageType()) + "\n"; print += "Sender = " + QString("%1").arg(this->sender()) + "\n"; print += "AlertType = " + QString("%1").arg(this->alertType()) + "\n"; print += "ID = " + QString("%1").arg(this->id()) + "\n"; print += "Origin = " + QString("%1").arg(this->origin()) + "\n"; print += "DataSource = " + QString("%1").arg(this->dataSource()) + "\n"; print += "Distance_To_Event = " + QString("%1").arg(this->distanceToEvent()) + "\n"; print += "Severity = " + QString("%1").arg(this->severity()) + "\n"; print += "Azimuth = " + QString("%1").arg(this->azimuth()) + "\n"; print += "Time_To_Event = " + QString("%1").arg(this->timeToEvent()) + "\n"; print += "DetailLevel = " + QString("%1").arg(this->detailLevel()) + "\n"; print += "FCWAvailable = " + QString("%1").arg(this->fcwReceived()) + "\n"; print += "RWWAvailable = " + QString("%1").arg(this->rwwReceived()) + "\n"; print += "EEBLAvailable = " + QString("%1").arg(this->eeblReceived()) + "\n"; print += "** AlertMessage end **\n"; return print; }
The output for my printout method:
QDateTime(2020-02-18 00:15:02.786 Mitteleuropäische Zeit Qt::LocalTime) "** AlertMessage begin**\nMessageType = ALERT\nSender = \nAlertType = FCW\nID = \nOrigin = Cohda_TA\nDataSource = \nDistance_To_Event = 55.2\nSeverity = 9\nAzimuth = 25.5434\nTime_To_Event = 20.3\nDetailLevel = 0\nFCWAvailable = 0\nRWWAvailable = 0\nEEBLAvailable = 0\n** AlertMessage end **\n"
Hope this helps somehow :)
-
I cannot see anything wrong in this code. All I can think is do a make clean, qmake, make project and see if this gets any better. I don't see any variables such as references exceeding their lifetime or anything. Sorry I cannot be more helpful. This should just work.
-
I think I figured out the issue but I don't know how to solve it. It's relying on this call in the main.cpp.
main.cpp
qmlRegisterType<AlertMessage>("io.qt.alertmessage", 1, 0, "AlertMessage");
This method uses the standard constructor of my AlertMessage class without the QJsonObject as a parameter. I need to define my custom constructor but I get stuck with some errors ...
If I don't use my custom constructor it will always initiliaze without any values set. -
@Thanni123 said in Exposing C++ values to QML issue:
If I don't use my custom constructor it will always initiliaze without any values set.
This is exactly what @Christian-Ehrlicher has ask you to do: always initialize your member variables in constructor this is for all constructors.
So if you add a call to clearMessage() in your default constructor, you will probably solve your issue.