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. can a slot be a global func?
Forum Updated to NodeBB v4.3 + New Features

can a slot be a global func?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 928 Views 2 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.
  • O Offline
    O Offline
    opengpu2
    wrote on last edited by
    #1

    can a slot be a global func?
    i want to write a timer and its slot...
    the slot should be a global func in main.cpp...
    how can i write the connect?
    thank you

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Slot should be a method inside a class that is derived from QObject. It can be global, I think.

      And with the new connection syntax in Qt 5, you can use any method (including a lambda function) as a slot: just pass the functor there.

      (Z(:^

      O 1 Reply Last reply
      0
      • sierdzioS sierdzio

        Slot should be a method inside a class that is derived from QObject. It can be global, I think.

        And with the new connection syntax in Qt 5, you can use any method (including a lambda function) as a slot: just pass the functor there.

        O Offline
        O Offline
        opengpu2
        wrote on last edited by
        #3

        @sierdzio It can be global, I think.
        but how connect to a global func?

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on last edited by sierdzio
          #4

          Let's see if that works:

          void myFunc() { qDebug() << "Aha!"; }
          int main ()
          {
              MyClass a;
              QObject::connect(&a, &MyClass::someSignal, &myFunc); // or just myFunc
              // ...
          }
          

          (Z(:^

          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