Unsolved Calling a variable from C++ in QML
-
-
thanks
do you mean like this:Q_PROPERTY(QString pNumber READ pNumber_read WRITE pNumber_write NOTIFY pNumber_changed)
and adding this as well:
public: QVariant pNumber_read() const; public slots: void pNumber_write(QString value); // i am not sure about this signals: void pNumber_changed(); private: QString m_pNumber;
-
Q_PROPERTY(QString pNumber READ pNumber WRITE setPNumber NOTIFY pNumberChanged)
public: const QString& pNumber() const {return m_pNumber;} void setPNumber(const QString& val){ if(val==m_pNumber) return; m_pNumber=val; pNumberChanged(m_pNumber); } signals: void pNumberChanged(const QString& pN); private: QString m_pNumber;
-
Pro (QtCreator) tip:
write
Q_PROPERTY(QString pNumber READ pNumber WRITE setPNumber NOTIFY pNumberChanged)
right click on
Q_PROPERTY
selectRefactor
click onGenerate Missing Q_Property Members
saves a lot of time
-
Great thank you,
but i still have this problem, that i cannot read the value in QML when i write this:
console.log (thepnumber.pNumber)
it shows nothing
-
@mamoud said in Calling a variable from C++ in QML:
Great thank you,
but i still have this problem, that i cannot read the value in QML when i write this:
console.log (thepnumber.pNumber)
it shows nothing
more context please, where exactly is this called?
//General solution
Connections{
target: thepnumberonpNumberChanged: console.log(thepnumber.pNumber)
} -
I have a QML called PageInfo.qml
Rectangle { id: infopage function pageEnter() { console.log("The part is: " + thepnumber.pNumber) }
-
-
Once i enter this page it will be called
-
@mamoud
if you writefunction pageEnter() {
console.log("Enter Function to get Number")
console.log("The part is: " + thepnumber.pNumber)
}do you see any console logs?
-
Did you set a value to
m_pNumber
? if you never assign anything to it than it's totally natural that the log shows an empty string. -
Yeah i see
Enter Function to get Number
and also i see
The part is:
but no value is shown for thepnumber.pNumber -
No but this m_pNumber should get values from a CAN signal (canalyzer) which is set in .cpp file