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. Calling multiple slots using a signal

Calling multiple slots using a signal

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 2.1k Views 3 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.
  • tomyT Offline
    tomyT Offline
    tomy
    wrote on last edited by tomy
    #1

    Hi all,

    Is there any better way to connect a signal to more than a slot than the one below, please?

    connect(sender1, SIGNAL(triggered()), this, SLOT(foo_1()));
    connect(sender1, SIGNAL(triggered()), this, SLOT(foo_2()));
    connect(sender1, SIGNAL(triggered()), this, SLOT(foo_3()));
    
    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Hi! Better in what regard?

      tomyT 1 Reply Last reply
      0
      • ? A Former User

        Hi! Better in what regard?

        tomyT Offline
        tomyT Offline
        tomy
        wrote on last edited by tomy
        #3

        @Wieland

        Hi! Better in what regard?

        Good question! I meant the way professionals use.

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          Well, you could save pointers to the slots in a vector and then do stuff like this:

              using foo_p =  void (MainWindow::*)();
              
              QVector<foo_p> fps;
              fps << &MainWindow::foo_1;
              fps << &MainWindow::foo_2;
              // ...
              fps << &MainWindow::foo_100;
          
              for (auto f: fps)
                  connect(this, &MainWindow::triggered, this, f);
          
          tomyT 1 Reply Last reply
          2
          • ? A Former User

            Well, you could save pointers to the slots in a vector and then do stuff like this:

                using foo_p =  void (MainWindow::*)();
                
                QVector<foo_p> fps;
                fps << &MainWindow::foo_1;
                fps << &MainWindow::foo_2;
                // ...
                fps << &MainWindow::foo_100;
            
                for (auto f: fps)
                    connect(this, &MainWindow::triggered, this, f);
            
            tomyT Offline
            tomyT Offline
            tomy
            wrote on last edited by
            #5

            @Wieland
            Not been taught these as they are rather advanced. For the time being I use the first version.
            Thanks.

            mrjjM 1 Reply Last reply
            0
            • tomyT tomy

              @Wieland
              Not been taught these as they are rather advanced. For the time being I use the first version.
              Thanks.

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by mrjj
              #6

              @tomy
              Hi
              What wieland shows is just normal c++.
              It is not considered advanced as such.
              But yes, you could say for a c++ beginner, function pointers might be advanced.
              He just put pointers to member functions in a list and
              loop over them to connect them all.
              If you had 200, it would be a much better design that a load of
              connect statements.

              Anyway, its not really common to connect same signal to different slot in same class. You might as well fall foo_2 and foo_3 from foo_1.

              But if the foo_x each lives in own classes, then its a pretty normal design.

              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