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. Is it possible to use a tuple and a loop for QObject::connect?
QtWS25 Last Chance

Is it possible to use a tuple and a loop for QObject::connect?

Scheduled Pinned Locked Moved Unsolved General and Desktop
connectorganization
5 Posts 4 Posters 1.0k 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.
  • S Offline
    S Offline
    Sailanarmo
    wrote on last edited by
    #1

    I was wondering if there was a way to clean up how many lines of code I have in the constructor while dealing with a connect.

    I was thinking of having a std::vector<std::tuple<QObject*,std::function<void()>,std::function<void()>>> connections;

    Where it would look like this in a private member of a class:

    const std::vector<std::tuple<QObject*,std::function<void()>,std::function<void()>>> connections =
    {
      std::make_tuple(mySlider, std::bind(&QSlider::sliderReleased,mySlider), std::bind(&Foo::onSliderChanged,this)),
      .
      .
      .
    };
    

    However, when I tried this doing:

    for (const auto &e : connections)
      connect(std::get<0>(e),SIGNAL(std::get<1>(e)),this,SLOT(std::get<2>(e)));
    

    None of the connections would connect, I would get an error like: No such slot Foo::std::get<1>(e).

    I thought it was a clever attempt. And honestly I am just wondering if there is a way to make it look cleaner instead of having 20+ lines eating up my constructor. It would be nice if I could do it this way somehow.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      SIGNAL is a macro. I think you are looking for the new-style connect overload that uses function pointers.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply
      6
      • SGaistS SGaist

        Hi,

        SIGNAL is a macro. I think you are looking for the new-style connect overload that uses function pointers.

        S Offline
        S Offline
        Sailanarmo
        wrote on last edited by
        #3

        @SGaist can you point me out in that direction? I would love to check that out.

        kshegunovK 1 Reply Last reply
        0
        • S Sailanarmo

          @SGaist can you point me out in that direction? I would love to check that out.

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          https://doc.qt.io/qt-5/signalsandslots.html

          The first example of QObject::connect, quite literally.

          Read and abide by the Qt Code of Conduct

          JKSHJ 1 Reply Last reply
          5
          • kshegunovK kshegunov

            https://doc.qt.io/qt-5/signalsandslots.html

            The first example of QObject::connect, quite literally.

            JKSHJ Offline
            JKSHJ Offline
            JKSH
            Moderators
            wrote on last edited by
            #5

            See also https://doc.qt.io/qt-5/signalsandslots-syntaxes.html for a comparison between the old and new connect() syntaxes.

            Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

            1 Reply Last reply
            4

            • Login

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