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. QMutex const/this conversion issue
QtWS25 Last Chance

QMutex const/this conversion issue

Scheduled Pinned Locked Moved General and Desktop
5 Posts 4 Posters 6.5k 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.
  • M Offline
    M Offline
    mirswith
    wrote on last edited by
    #1

    I am not sure what it is going on here, I have cleaned my project and I can't get this to go away in one of my classes. I have declared a private variable:

    @
    QMutex _listMutex;
    @

    And then I go to use it:

    @
    QMutexLocker lock(&_listMutex);
    @

    When I compile this one gives me:
    @
    SomeFile.cpp:111: error: C2664: 'QMutexLocker::QMutexLocker(QMutex *)' : cannot convert parameter 1 from 'const QMutex *' to 'QMutex *'
    Conversion loses qualifiers
    @

    And elsewhere:

    @
    _listMutex.lock();
    ...
    _listMutex.unlock();
    @

    and this gives me the compile errors:
    @
    SomeFile.cpp:289: error: C2662: 'QMutex::lock' : cannot convert 'this' pointer from 'const QMutex' to 'QMutex &'
    Conversion loses qualifiers
    SomeFile.cpp:291: error: C2662: 'QMutex::unlock' : cannot convert 'this' pointer from 'const QMutex' to 'QMutex &'
    Conversion loses qualifiers
    @

    Yet I am using this exactly the same way in another class. What is going on?

    Thanks.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      baysmith
      wrote on last edited by
      #2

      You are probably trying to lock/unlock the mutex inside a const member function. Since the lock/unlock functions on QMutex are non-const, the compiler complains. Change _listMutex to mutable.

      @
      mutable QMutex _listMutex;
      @

      Nokia Certified Qt Specialist.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dangelog
        wrote on last edited by
        #3

        Can you pastebin the whole segment? Are you trying to lock a mutex that you have as member from a const method? (If so, use mutable).

        Software Engineer
        KDAB (UK) Ltd., a KDAB Group company

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mirswith
          wrote on last edited by
          #4

          that was it. Just when one thinks they are getting the hang of const. :)

          Thanks!

          1 Reply Last reply
          0
          • D Offline
            D Offline
            deimos
            wrote on last edited by
            #5

            try declaring the mutex like

            @mutable QMutex _listMutex;@

            I am not sure, just a tought

            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