PDA

View Full Version : Connect SINAGLS?SLOTS



jesse_mark
21st November 2012, 16:24
Hello Guys,

Im trying to Connect multiple QlineEdit::textChanged(QString) SIGNAL with one SLOT , but in this slot i need it to change the Label color of the particular LineEdit that trigger the Slot.

so how can i connect a textchanged signal correctly to the slot ??

the problem the slot signature is not the same the as signal signature, which i know that u can not connect them this way.

but can i change the signal signature ?? because i need to know the lable for that particular lineEdit.

i'll try to illustrate more by this exmple:






connect(ui->LEdit1, SIGNAL(textChanged(QString)), this, SLOT(setLableColor(..,.,))); // change lable1 color

connect(ui->LEdit2, SIGNAL(textChanged(QString)), this, SLOT(setLableColor(.,.,.))); //change lable2 color

connect(ui->LEdit3, SIGNAL(textChanged(QString)), this, SLOT(setLableColor(.,.,.))); //change lable3 color

connect(ui->LEdit4, SIGNAL(textChanged(QString)), this, SLOT(setLableColor(.,.,.))); //change lable4 color

connect(ui->LEdit5, SIGNAL(textChanged(QString)), this, SLOT(setLableColor(.,.,.))); //change lable5 color



void setLableColor(QLabel *lable, QString txt , QString color)
{
//change the lable color ;
}



before i used to have a textchange slot for each lineEdit and then call the setLableColor from there. but i though maybe is should be a way to just have one slot and connect all the LineEdit to this as im doing the same thing its just a different label.
so, is there a way to connect with this slot ?? or how should i do it ??


Thank you so much in advnace for any help and suggestions.

Santosh Reddy
21st November 2012, 16:35
you can get the QLineEdit which emited the signal inside the slot using sender() function. Here is an example with single slot



connect(ui->LEdit1, SIGNAL(textChanged(QString)), this, SLOT(setColor(QString)));
connect(ui->LEdit2, SIGNAL(textChanged(QString)), this, SLOT(setColor(QString)));
connect(ui->LEdit3, SIGNAL(textChanged(QString)), this, SLOT(setColor(QString)));
connect(ui->LEdit4, SIGNAL(textChanged(QString)), this, SLOT(setColor(QString)));
connect(ui->LEdit5, SIGNAL(textChanged(QString)), this, SLOT(setColor(QString)));

void setColor(QString txt)
{
QLineEdit * LEdit = dynamic_cast<QLineEdit *>(sender());
if(LEdit != 0)
{
//change the lable color ;
}
}

jesse_mark
21st November 2012, 16:57
Thank you so much,

Sorry, but how i know which LineEdit is this ??? i mean, how would i deiced is the sender for example is LEdit1 not LEdit2 ??

PS: do u think its better to do it this way, where i detect which lineEdit emit the signal and change the lable color.
or i keep using a different slot for each lineEdit ??

Lesiok
21st November 2012, 17:12
Use QObject::sender or QSignalMapper

Santosh Reddy
21st November 2012, 19:24
having single slot for multiple QLineEdit will be a nice approach.

amleto
21st November 2012, 19:35
I would say a better way is to treat the label + line edit as an 'entity'

e.g.


class LabelAndEdit
{
public:
LabelAndEdit()
: edit(new QLineEdit), label(new QLabel)
{
label->setBuddy(edit);
connect(edit, SIGNAL(textChanged(QString)), this, SLOT(setColor(QString)));
}

QLineEdit* lineEdit() const {return edit;}
QLabel* label() const {return label;}

public slots:
void setColor(QString color)
{
// set color on label
}

private:
QLineEdit* edit;
QLabel* label;
}

This is much better encapsulation. You could modify it to use existing QLineEdit etc. If you are using the designer and coming up with things like lineedit1, lineedit2, lineedit3, lineedit4, ... then it will lead to messy code. You should get used to 'designing' in code and it will be much cleaner and simpler.

jesse_mark
21st November 2012, 20:54
Thanks y'all for the useful hints.

Amleto, Yes i do use the designer, but of coruse i dont use lineedit1,...lineeditn to name my objects,I do use meaningful names.

I like your lableAndEdit approach, would not come to my mind :)

Why it is better to design in code ? is using the designer is a bad habit ??
I am using the designer because its much easier and clearer and much faster, especially if i have a lot of widgets to layout, where much faster to deal with the size, layouts, adding and modifying.
I think using the designer is more cleaner and simpler than using the code, where all the setting the such as size, color, font, layouts, location, .... and many other attributes can be set and be hidden in the code and easily modified from the designer.

I am new and i am still learning, so i don't know why using the code is better unless in cases where i need to create a especial use widget, as the LabelAndEdit you show me now.

I think I will post this is as a question, so I would know more and new noob like me will now that too.

Thank you so much,

amleto
21st November 2012, 22:05
Amleto, Yes i do use the designer, but of coruse i dont use lineedit1,...lineeditn to name my objects,I do use meaningful names.
The point is not the suitability of names, but the fact that there are so many.

If you design in designer, and then want to link lineedits to labels, you have to 'hard code' it using the variable names you gave each individual element in the designer.

If you do it in code you don't need to worry about variable names and you can set up a short factory pattern or loop that can deal with any number of elements.

e.g.

you have designed a form with a bunch of lineedits and then you need to connect them in a class ctor you end up with code like this



classX::classX()
{
QWidget* w1 = new QWidget;
connect(ui->lineedit1, SIGNAL(...), w1, SLOT(...));

QWidget* w2 = new QWidget;
connect(ui->lineedit2, SIGNAL(...), w1, SLOT(...));

QWidget* w3 = new QWidget;
connect(ui->lineedit3, SIGNAL(...), w1, SLOT(...));

QWidget* w4 = new QWidget;
connect(ui->lineedit4, SIGNAL(...), w1, SLOT(...));

QWidget* w5 = new QWidget;
connect(ui->lineedit5, SIGNAL(...), w1, SLOT(...));
}


c.f.



classX::classX()
{
for (int i = 0; i < N; ++i)
{
QLineEdit* le = new QLineEdit;
QWidget* w = new QWidget;
connect(le, SIGNAL(...), w, SLOT(...));
}
}