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 are parameters referenced when using QTimer::singleShot?
QtWS25 Last Chance

How are parameters referenced when using QTimer::singleShot?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 3 Posters 330 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
    mirro
    wrote on 11 Dec 2021, 12:54 last edited by mirro 12 Nov 2021, 12:55
    #1

    void fun(QString& str)
    {

    }
    QString param;
    QTimer::singleShot(200,this,[&,param](){}
    };
    //error:qualifiers dropped in binding reference of type "QString &" to initializer of type "const QString"

    J 1 Reply Last reply 11 Dec 2021, 13:12
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 11 Dec 2021, 15:02 last edited by Chris Kawa 12 Nov 2021, 15:06
      #3

      Did you meant to write QTimer::singleShot(200,this,[&,param](){ fun(param); }); i.e. actually call the fun function from the lambda?

      If so, lambdas are immutable by default, which means param becomes const when copied by the capture group. You can't then use it as a non const parameter to fun. Couple of ways to fix this:

      • If you don't want to modify str then make it a const parameter i.e. void fun(const QString& str).

      • If you want to modify the original parameter capture it by reference, not by value i.e.
        [&](){ fun(param); } or explicitly [&param](){ fun(param); }

      • If you want to capture param by copy and modify that copy then you have to make the lambda mutable
        [=]() mutable { fun(param); } or explicitly [param]() mutable { fun(param); }
        There's usually no reason to do that, but if you really want to you can.

      1 Reply Last reply
      3
      • M mirro
        11 Dec 2021, 12:54

        void fun(QString& str)
        {

        }
        QString param;
        QTimer::singleShot(200,this,[&,param](){}
        };
        //error:qualifiers dropped in binding reference of type "QString &" to initializer of type "const QString"

        J Offline
        J Offline
        JonB
        wrote on 11 Dec 2021, 13:12 last edited by
        #2

        @mirro
        Assuming something close to this is your actual code (since above won't compile), I can only guess there is an issue/confusion about passing both & ("by-reference") and param ("not-by-reference"); do you really need this?

        Otherwise can you show actual code you are compiling.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on 11 Dec 2021, 15:02 last edited by Chris Kawa 12 Nov 2021, 15:06
          #3

          Did you meant to write QTimer::singleShot(200,this,[&,param](){ fun(param); }); i.e. actually call the fun function from the lambda?

          If so, lambdas are immutable by default, which means param becomes const when copied by the capture group. You can't then use it as a non const parameter to fun. Couple of ways to fix this:

          • If you don't want to modify str then make it a const parameter i.e. void fun(const QString& str).

          • If you want to modify the original parameter capture it by reference, not by value i.e.
            [&](){ fun(param); } or explicitly [&param](){ fun(param); }

          • If you want to capture param by copy and modify that copy then you have to make the lambda mutable
            [=]() mutable { fun(param); } or explicitly [param]() mutable { fun(param); }
            There's usually no reason to do that, but if you really want to you can.

          1 Reply Last reply
          3

          3/3

          11 Dec 2021, 15:02

          • Login

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