Calling a variable from C++ in QML
-
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 Memberssaves a lot of time
-
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; -
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) } -
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.