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. We konw updating widget in thread is not safe, but if i read widget in thread ,is it safe?
Forum Updated to NodeBB v4.3 + New Features

We konw updating widget in thread is not safe, but if i read widget in thread ,is it safe?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 428 Views 1 Watching
  • 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.
  • Q Offline
    Q Offline
    QtTester
    wrote on last edited by
    #1

    assume in a thread, ui->label->setText("abc") is not safe, as we know.

    but if i just read the text in a thread ,is it safe now?

    QThread::run()
    {
    Mainwindow p = qobject_cast<Mainwindow>(parent);
    p->readText();
    }

    and the func is:

    QString Mainwindow::readText()
    {
    QString str = ui->label->text();
    return str;
    }

    JKSHJ 1 Reply Last reply
    0
    • Q QtTester

      assume in a thread, ui->label->setText("abc") is not safe, as we know.

      but if i just read the text in a thread ,is it safe now?

      QThread::run()
      {
      Mainwindow p = qobject_cast<Mainwindow>(parent);
      p->readText();
      }

      and the func is:

      QString Mainwindow::readText()
      {
      QString str = ui->label->text();
      return str;
      }

      JKSHJ Online
      JKSHJ Online
      JKSH
      Moderators
      wrote on last edited by
      #2

      @QtTester said in We konw updating widget in thread is not safe, but if i read widget in thread ,is it safe?:

      but if i just read the text in a thread ,is it safe now?

      No.

      The GUI thread could change the text while your other thread tries to read the text. So your other thread could try to read string data that has already been freed.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      Q 1 Reply Last reply
      4
      • JKSHJ JKSH

        @QtTester said in We konw updating widget in thread is not safe, but if i read widget in thread ,is it safe?:

        but if i just read the text in a thread ,is it safe now?

        No.

        The GUI thread could change the text while your other thread tries to read the text. So your other thread could try to read string data that has already been freed.

        Q Offline
        Q Offline
        QtTester
        wrote on last edited by
        #3

        @JKSH it may not get the right data, but the app should not crash ,right?

        JKSHJ 1 Reply Last reply
        0
        • Q QtTester

          @JKSH it may not get the right data, but the app should not crash ,right?

          JKSHJ Online
          JKSHJ Online
          JKSH
          Moderators
          wrote on last edited by JKSH
          #4

          @QtTester said in We konw updating widget in thread is not safe, but if i read widget in thread ,is it safe?:

          @JKSH it may not get the right data, but the app should not crash ,right?

          If it tries to read a string that is being destroyed, it can crash.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          Q 1 Reply Last reply
          1
          • JKSHJ JKSH

            @QtTester said in We konw updating widget in thread is not safe, but if i read widget in thread ,is it safe?:

            @JKSH it may not get the right data, but the app should not crash ,right?

            If it tries to read a string that is being destroyed, it can crash.

            Q Offline
            Q Offline
            QtTester
            wrote on last edited by
            #5

            @JKSH In a app lifetime ,the qlabel will be destroyed after the qthread ( or we guarantee that), so i donot think it will crash.

            JKSHJ 1 Reply Last reply
            0
            • Q QtTester

              @JKSH In a app lifetime ,the qlabel will be destroyed after the qthread ( or we guarantee that), so i donot think it will crash.

              JKSHJ Online
              JKSHJ Online
              JKSH
              Moderators
              wrote on last edited by JKSH
              #6

              @QtTester said in We konw updating widget in thread is not safe, but if i read widget in thread ,is it safe?:

              @JKSH In a app lifetime ,the qlabel will be destroyed after the qthread ( or we guarantee that), so i donot think it will crash.

              I said the string, not the QLabel.

              QLabel::setText() can destroy the old string. So if your GUI thread calls label->setText(...) at the same time that your other thread calls label->text(), bad things can happen.

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              Q 1 Reply Last reply
              4
              • JKSHJ JKSH

                @QtTester said in We konw updating widget in thread is not safe, but if i read widget in thread ,is it safe?:

                @JKSH In a app lifetime ,the qlabel will be destroyed after the qthread ( or we guarantee that), so i donot think it will crash.

                I said the string, not the QLabel.

                QLabel::setText() can destroy the old string. So if your GUI thread calls label->setText(...) at the same time that your other thread calls label->text(), bad things can happen.

                Q Offline
                Q Offline
                QtTester
                wrote on last edited by
                #7

                @JKSH Thanks a lot.

                1 Reply Last reply
                0

                • Login

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