PDA

View Full Version : Connecting many SIGNALS to SLOTS



Alex22
2nd December 2015, 08:26
Hi
I have 100 Check-boxes (objects of QCheckBox) and 100 Line-edits (objects of QLineEdit) like this:


QCheckBox chb[100];
QLineEdit lndt[100];

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:


QObject::connect(chb, SIGNAL(textChanged(QString)), lndt, SLOT(setText(QString)));
QObject::connect(chb+1, SIGNAL(textChanged(QString)), lndt+1, SLOT(setText(QString)));
QObject::connect(chb+2, SIGNAL(textChanged(QString)), lndt+2, SLOT(setText(QString)));
.
.
.
QObject::connect(chb+99, SIGNAL(textChanged(QString)), lndt+99, SLOT(setText(QString)));


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

Vikram.Saralaya
2nd December 2015, 09:15
for(int i=0; i<100; ++i)
QObject::connect(&chb[i], SIGNAL(stateChanged(int)), &lndt[i], SLOT(setText(QString));

Ps: QCheckbox emits stateChanged signal not textChanged

yeye_olive
2nd December 2015, 09:18
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.

Alex22
2nd December 2015, 09:31
thanks a lot Vikram.Saralaya


QWidget wdg;
QLabel lbl[100];
QLineEdit le[100];
QGridLayout lay(&wdg);
for(int i=0 ; i<10; i++)
{
lay.addWidget(lbl+i,i,0);
lay.addWidget(le+i,i,1);
QObject::connect(le+i, SIGNAL(textChanged(QString)), lbl+i, SLOT(setText(QString)));
}

wdg.show();