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. Using Arguments with SingleShotTimer
Forum Updated to NodeBB v4.3 + New Features

Using Arguments with SingleShotTimer

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 331 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.
  • L Offline
    L Offline
    leinad
    wrote on last edited by
    #1

    Hi,

    Is it possible to execute a method which has arguments launched by a single shot timer?

    QTimer::singleShot(0, this, SLOT(processMessageBox(arg1, arg2)));

    I tried using lamda functions but can't seem to get it to work. If there is a way, can you please provide an example?

    Thanks in advance!

    J.HilkJ 1 Reply Last reply
    0
    • L leinad

      Hi,

      Is it possible to execute a method which has arguments launched by a single shot timer?

      QTimer::singleShot(0, this, SLOT(processMessageBox(arg1, arg2)));

      I tried using lamda functions but can't seem to get it to work. If there is a way, can you please provide an example?

      Thanks in advance!

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

      @leinad sure, lambda is the way to go, assuming arg1 and arg2 are actual variable names

      QTimer::singleShot(0, this, [=]()->void{
           //= by-copy capture default
          processMessageBox(arg1, arg2);
      });
      

      that said, a single shot time is a bit heavy weight for a 0 timeout, I would recommend QMetaObject::invokeMethod

      QMetaObject::invokeMethod(this, [=]()->void{
           //= by-copy capture default
          processMessageBox(arg1, arg2);
      }, Qt::QueuedConnection // same effect as 0 timeout of a Qtimer
      );
      

      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
      3
      • L Offline
        L Offline
        leinad
        wrote on last edited by
        #3

        Great thanks!. Let me give it a try :)

        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