Results 1 to 4 of 4

Thread: Connecting many SIGNALS to SLOTS

  1. #1
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Connecting many SIGNALS to SLOTS

    Hi
    I have 100 Check-boxes (objects of QCheckBox) and 100 Line-edits (objects of QLineEdit) like this:

    Qt Code:
    1. QCheckBox chb[100];
    2. QLineEdit lndt[100];
    To copy to clipboard, switch view to plain text mode 

    now I want to connect chb[0] to lndt[0] and chb[1] to lndt[1] and chb[2] to lndt[2] ,... and chb[99] to lndt[99]

    One way is to write 100 times connects like:
    Qt Code:
    1. QObject::connect(chb, SIGNAL(textChanged(QString)), lndt, SLOT(setText(QString)));
    2. QObject::connect(chb+1, SIGNAL(textChanged(QString)), lndt+1, SLOT(setText(QString)));
    3. QObject::connect(chb+2, SIGNAL(textChanged(QString)), lndt+2, SLOT(setText(QString)));
    4. .
    5. .
    6. .
    7. QObject::connect(chb+99, SIGNAL(textChanged(QString)), lndt+99, SLOT(setText(QString)));
    To copy to clipboard, switch view to plain text mode 


    Is there any other idea for using a for loop to do this.
    Thanks a lot for any help

  2. #2
    Join Date
    May 2015
    Posts
    66
    Thanks
    10
    Thanked 17 Times in 17 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Connecting many SIGNALS to SLOTS

    Qt Code:
    1. for(int i=0; i<100; ++i)
    2. QObject::connect(&chb[i], SIGNAL(stateChanged(int)), &lndt[i], SLOT(setText(QString));
    To copy to clipboard, switch view to plain text mode 

    Ps: QCheckbox emits stateChanged signal not textChanged

  3. The following user says thank you to Vikram.Saralaya for this useful post:

    Alex22 (2nd December 2015)

  4. #3
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Connecting many SIGNALS to SLOTS

    Of course you can use a for loop. This is basic C++. Just look at arguments 1 and 3 of your calls to QObject::connect(); there is an obvious pattern. If you know how to iterate over an array with a for loop, then you can surely figure out how do the same over two arrays in lockstep.

  5. The following user says thank you to yeye_olive for this useful post:

    Alex22 (2nd December 2015)

  6. #4
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Connecting many SIGNALS to SLOTS

    thanks a lot Vikram.Saralaya
    Qt Code:
    1. QWidget wdg;
    2. QLabel lbl[100];
    3. QLineEdit le[100];
    4. QGridLayout lay(&wdg);
    5. for(int i=0 ; i<10; i++)
    6. {
    7. lay.addWidget(lbl+i,i,0);
    8. lay.addWidget(le+i,i,1);
    9. QObject::connect(le+i, SIGNAL(textChanged(QString)), lbl+i, SLOT(setText(QString)));
    10. }
    11.  
    12. wdg.show();
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Connecting signals and slots from different process
    By Momergil in forum Qt Programming
    Replies: 3
    Last Post: 8th February 2014, 16:59
  2. Connecting QML signals with Qt slots
    By KIBSOFT in forum Qt Quick
    Replies: 1
    Last Post: 15th November 2010, 10:18
  3. Replies: 8
    Last Post: 18th July 2009, 16:57
  4. Connecting signals and slots help pls
    By bod in forum Qt Programming
    Replies: 9
    Last Post: 1st July 2008, 16:01
  5. Connecting signals & slots across different threads
    By jyoti kumar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 13:40

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.