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 last edited by mirro
    #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"

    JonBJ 1 Reply Last reply
    0
    • Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #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

        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"

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on 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
        • Chris KawaC Online
          Chris KawaC Online
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by Chris Kawa
          #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

          • Login

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