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. Is QSettings thread safe?
Forum Updated to NodeBB v4.3 + New Features

Is QSettings thread safe?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 1.0k Views 3 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.
  • A Offline
    A Offline
    Alexey Serebryakov
    wrote on last edited by
    #1

    Hi there,
    I found in our legacy project following code

    void Foo::save() {
    QSettings settings;
    
    if (something) {
        QReadLocker locker(&lock_); // is valid to use lock?
        settings->setValue("widgetState", widgetStateValue);
        ...
        settings->sync();
    }
    }
    

    Thanks a lot!

    J.HilkJ 1 Reply Last reply
    0
    • A Alexey Serebryakov

      Hi there,
      I found in our legacy project following code

      void Foo::save() {
      QSettings settings;
      
      if (something) {
          QReadLocker locker(&lock_); // is valid to use lock?
          settings->setValue("widgetState", widgetStateValue);
          ...
          settings->sync();
      }
      }
      

      Thanks a lot!

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @Alexey-Serebryakov

      https://doc.qt.io/qt-5/qsettings.html

      5a890952-35ad-48dd-8b91-e4b316636a7b-image.png


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      2
      • jeremy_kJ Online
        jeremy_kJ Online
        jeremy_k
        wrote on last edited by
        #3

        Note the :, and the brief list that follows it.

        Asking a question about code? http://eel.is/iso-c++/testcase/

        A 1 Reply Last reply
        1
        • jeremy_kJ jeremy_k

          Note the :, and the brief list that follows it.

          A Offline
          A Offline
          Alexey Serebryakov
          wrote on last edited by
          #4

          @jeremy_k So, that mean is read/write methods of QSettings are not thread-safe against only registerFormat?

          jeremy_kJ kshegunovK 2 Replies Last reply
          0
          • A Alexey Serebryakov

            @jeremy_k So, that mean is read/write methods of QSettings are not thread-safe against only registerFormat?

            jeremy_kJ Online
            jeremy_kJ Online
            jeremy_k
            wrote on last edited by
            #5

            @Alexey-Serebryakov said in Is QSettings thread safe?:

            @jeremy_k So, that mean is read/write methods of QSettings are not thread-safe against only registerFormat?

            Yes, that is what the documentation says.

            QSettings is derived from QObject, so in general an instance should only be used from the thread it is associated with.

            Asking a question about code? http://eel.is/iso-c++/testcase/

            1 Reply Last reply
            3
            • A Alexey Serebryakov

              @jeremy_k So, that mean is read/write methods of QSettings are not thread-safe against only registerFormat?

              kshegunovK Offline
              kshegunovK Offline
              kshegunov
              Moderators
              wrote on last edited by
              #6

              Any particular reason you want to create a global settings object?
              ... and related question, how do you construct said global object?

              Read and abide by the Qt Code of Conduct

              jeremy_kJ 1 Reply Last reply
              0
              • kshegunovK kshegunov

                Any particular reason you want to create a global settings object?
                ... and related question, how do you construct said global object?

                jeremy_kJ Online
                jeremy_kJ Online
                jeremy_k
                wrote on last edited by
                #7

                @kshegunov said in Is QSettings thread safe?:

                Any particular reason you want to create a global settings object?

                There's a discrepancy between the thread title and the post body. The title indicates a concern about threading. The function in OP shows a function local QSettings object, and no obvious way for it to be used in a different thread.

                Using QReadLocker prior to QSettings::setValue() and sync() is curious.

                Asking a question about code? http://eel.is/iso-c++/testcase/

                kshegunovK 1 Reply Last reply
                0
                • jeremy_kJ jeremy_k

                  @kshegunov said in Is QSettings thread safe?:

                  Any particular reason you want to create a global settings object?

                  There's a discrepancy between the thread title and the post body. The title indicates a concern about threading. The function in OP shows a function local QSettings object, and no obvious way for it to be used in a different thread.

                  Using QReadLocker prior to QSettings::setValue() and sync() is curious.

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #8

                  @jeremy_k said in Is QSettings thread safe?:

                  There's a discrepancy between the thread title and the post body.

                  Indeed. I just ignored the local variable as it didn't make sense to me :P

                  Read and abide by the Qt Code of Conduct

                  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