Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to get m_name data to the Text which is written in mainrootwindow.qml
Forum Updated to NodeBB v4.3 + New Features

how to get m_name data to the Text which is written in mainrootwindow.qml

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 154 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    postbox
    wrote on last edited by postbox
    #1

    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. :)

    1 Reply Last reply
    0
    • P Offline
      P Offline
      postbox
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • P postbox has marked this topic as solved on

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved