how to get m_name data to the Text which is written in mainrootwindow.qml
-
i want to get the data from assigned m_name(string) in cpp file to the mainrootwindow qml file. But it dont show up the in the text.
here is my cpp file.void ....................{ if(searchName.exec()){ while (searchName.next()) { user_name_from_db = searchName.value(0).toString(); m_name = user_name_from_db; } qDebug()<<("username "+m_name); } QString Database::name() const { return m_name; } void Database::setName(const QString &newName) { if (m_name == newName) return; m_name = newName; emit nameChanged(); }
here is my .h file
class Database : public QObject { Q_OBJECT public: explicit Database(QObject *parent = nullptr); Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) QString name() const; void setName(const QString &newNamsignals: signals: void nameChanged(); private: QString m_name; };
and here is my .qml file
Text{ id: username text: qsTr(database.name) color : "#F25822" font.pointSize: 9 font.bold: true anchors.left: parent.right anchors.top: parent.top anchors.topMargin: 20 }
In the console it displayed what m_name is stored like "username King"....but when i build, the text doesn't show. Could Anyone please explain is there anything wrong what i did, or what i have to do?
Thanks in Advance. :)
-
It works now....the reason is when i build the program the m_name stores the default value as "Null". Then i give some input after build, the function don't call where the text is called. So it always show the Null value(which is empty text). Here's how i solved it by using the timer to call the function.
Text{ id: username text: qsTr(database.name) color : "#F25822" font.pointSize: 9 font.bold: true anchors.left: parent.right anchors.top: parent.top anchors.topMargin: 20 } Timer { interval: 500; running: true; repeat: true onTriggered: username.text = database.name }
Hope this will help in future who have same issue.
-