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 to use one function to connect 10 QLabels?
Qt 6.11 is out! See what's new in the release blog

How to use one function to connect 10 QLabels?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 1.3k Views 1 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.
  • M Offline
    M Offline
    MasterBlade
    wrote on last edited by
    #1

    Hello, I have 10 QLabels and I want to write context menus for them. But I found out that I need to write 10 connect functions. Is there a way to combine them so that I could write less?

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

      Hi,

      Make a vector of these labels and use a for loop with the connect statement in it.

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

      VRoninV 1 Reply Last reply
      3
      • mranger90M Offline
        mranger90M Offline
        mranger90
        wrote on last edited by
        #3

        You might want to check out QSignalMapper

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          Make a vector of these labels and use a for loop with the connect statement in it.

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          @SGaist said in How to use one function to connect 10 QLabels?:

          Make a vector of these labels and use a for loop with the connect statement in it.

          You can even create it on the fly:

          for(QLabel* singleLabel : {label1,label2,label3,label4}){
              QObject::connect(singleLabel,&QLabel::customContextMenuRequested,/*etc*/);
          }
          

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          2

          • Login

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