Dynamical LineEdit and signals
Hi all,
i have a problem, with a loop I create several LineEdits and signals, depending of the content of a list that stores data from a XML.
The problem is that the user can modify the content of the different LineEdits, but i do not know how to recognize which LineEdit has been modified, because it was created by a loop.
Code:
void Controller_run::update_running_list(Q3ListViewItem *s){
connect (value, SIGNAL (editingFinished()),this, SLOT (Actualize()));
value->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
value
->setMinimumSize
(QSize(71,
20));
value
->setMaximumSize
(QSize(71,
20));
name->setText(s->text(0));
value->setText(s->text(4));
if( s->text(1) == "DISCRETE_IN" || s->text(1) == "ANALOG_IN"){
value->setDisabled("true");
gridlayout->addWidget(name,row_input, 2);
gridlayout->addWidget(value, row_input, 3);
}
else{
value->setEnabled("true");
gridlayout->addWidget(name,row_output, 0);
gridlayout->addWidget(value, row_output, 1);
}
}
Please, any idea? Thank you!
Re: Dynamical LineEdit and signals
in update_running_list() make a list of your QLineEdits, you need it also because you want to be able to delete them , otherwise you get potential memory leak.
Code:
//in Controller_run.h
QVector<QLineEdit*> m_vecLineEdit;
Code:
void Controller_run::update_running_list(Q3ListViewItem *s){
m_vecLineEdit.push_back(value );
.....
in the slot Actualize() you can do something like:
Code:
for(int i=0; i< m_vecLineEdit.size(); i++){
if(m_vecLineEdit[i] == pLineEdit){
//now you know which line edit you have and you can do what you want with it.
}
}
and, clean up:
Code:
Controller_run::~Controller_run()
{
for(int i=0; i< m_vecLineEdit.size(); i++){
delete m_vecLineEdit[i];
}
}
Re: Dynamical LineEdit and signals
Ok, than you very much high_flyer, when a i compile this errors occurs refered to QLineEdit *pLineEdit = sender();
Controller_run.cpp(257) : error C2440: 'initializing' : cannot convert from 'QObject *' to 'QLineEdit *'
1> Cast from base to derived requires dynamic_cast or static_cast
Thank you very much again.
Re: Dynamical LineEdit and signals
Code:
QLineEdit *pLineEdit
= qobject_cast<QLineEdit
*>
(sender
());
That should do the trick. You need to cast the return value of sender() which is a generic QObject* to the type of object you are expecting the sender to be - QLineEdit*
Re: Dynamical LineEdit and signals
Another way would be to use QSignalMapper, so you assign the event you want to catch to the mapper along with an id, and when such an event occurs, the mapper will call your slot with that id. That way you don't need to use a for loop or do any casting to find out which object fired the signal.
Makes your code easier to read too, and is more OO.
Re: Dynamical LineEdit and signals
I agree with fatjuicymole in this case.
QSignalMapper would fit better in here.