There is data that is updated at a constant speed, in addition there is an Image element that is shown, or disappears, depending on the data received.

In my case, Image receives data once and for all. When the program is running, changing the data Image does not respond.

How to make the data perceived element Image in real time?

 Image { id: indicator1 x: 320 y: 257 fillMode: Image.PreserveAspectFit source: "../images/grn.png" visible: stringList[0] === "0" || stringList[0] === "1" ? true : false } 

In *.cpp

 void SerialPort::processSingleRecord (QByteArray value) { QString value2 = QString(value).trimmed(); QStringList sl = value2.split(","); QString qqq; std::vector<QString> fullData; fullData.reserve(sl.size()+22); for(int i = 0; i < sl.size()-1; i++) fullData.push_back(sl[i]); QString lastItem = sl[sl.size()-1]; for(int i = 0; i < lastItem.size(); i++) fullData.push_back(QString(lastItem[i])); for(int i = 0; i <fullData.size(); i++) qqq+= fullData[i]+','; set_serial_data(qqq); } 

In *.qml

  SerialPort { id: cppClass } property variant stringList: cppClass.serial_data.split(',') Image { id: indicator1 x: 320 y: 257 fillMode: Image.PreserveAspectFit source: "../images/grn.png" visible: stringList[0] === "0" || stringList[0] === "1" ? true : false } 
  • The ternary operator for visible is redundant, and in fact you need to change the visibility of the image where you get the corresponding data using the image component id indicator1 - Alexander Chernin
  • I get the data in the .cpp file, and in qml I'm already referring to the right element .... - user321152
  • Display the data transfer code from cpp to qml - Alexander Chernin
  • In * .cpp, void SerialPort :: processSingleRecord (QByteArray value) {QString value2 = QString (value) .trimmed (); QStringList sl = value2.split (","); QString qqq; std :: vector <QString> fullData; fullData.reserve (sl.size () + 22); for (int i = 0; i <sl.size () - 1; i ++) fullData.push_back (sl [i]); QString lastItem = sl [sl.size () - 1]; for (int i = 0; i <lastItem.size (); i ++) fullData.push_back (QString (lastItem [i])); for (int i = 0; i <fullData.size (); i ++) qqq + = fullData [i] + ','; set_serial_data (qqq); } In * .qml SerialPort {id: cppClass} property variant stringList: cppClass.serial_data.split (',') - user321152
  • Better add code to the question, otherwise nothing is clear - Alexander Chernin

1 answer 1

And so, you have a class SerialPort , in the method of which processSingleRecord you process incoming data from some serial port. As a result of processing, you form a string list whose first element can be either 0 or 1 .

 // SerialPort.cpp class SerialPort { public: signals: void stateChanged(QString value); // Π‘ΠΈΠ³Π½Π°Π»ΠΈΠ·ΠΈΡ€ΡƒΠ΅ΠΌ Ρ‚ΠΎΠ»ΡŒΠΊΠΎ ΠΎ ΠΏΠ΅Ρ€Π²ΠΎΠΌ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠΈ списка, Ρ‡Ρ‚ΠΎ Π΄Π΅Π»Π°Ρ‚ΡŒ с ΠΎΡΡ‚Π°Π»ΡŒΠ½Ρ‹ΠΌΠΈ Π²Ρ‹ Ρ€Π΅ΡˆΠΈΡ‚Π΅ сами private slots: void processSingleRecord(QByteArray data) { //... emit stateChanged( fullData[0] ); } } 

Register the SerialPort class in main.cpp , for example:

 //... qmlRegisterType<SerialPort>("ru.user321152", 1, 0, "SerialPort"); // ... 

Go to main.qml .

 import ru.user321152.SerialPort 1.0 ApplicationWindow { Image { id: indicator1; } SerialPort { onStateChanged: { // ΠΎΠ±Ρ€Π°Ρ‚ΠΈΡ‚Π΅ Π²Π½ΠΈΠΌΠ°Π½ΠΈΠ΅ Π½Π° прСфикс on // value - строковоС Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ состояния indicator1.visible = value === "1" } } } 
  • so, with one value, everything worked for me ... onStateChanged: {indicator1.visible = stringList [6] === "0"} and how do I now make it so that onStateChanged works like this: {indicator1.visible = stringList [ 6] === "0" indicator1.visible = stringList [7] === "1" indicator1.visible = stringList [8] === "0" indicator1.visible = stringList [9] === "1" } - user321152
  • I do not understand the question. Does the light bulb depend on several states? - Alexander Chernin
  • I have 10 light bulbs .... - user321152
  • Formulate your problem and ask a new question. I or someone else will try to answer, otherwise it’s not very convenient for me, because I still have to write code - Alexander Chernin
  • But no, my version Works with your code ... everything is fine. Tested while 3 lamps, each lit depending on the signal that comes. - user321152