PDA

View Full Version : Dynamical LineEdit and signals



martisho
10th November 2009, 10:24
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.


void Controller_run::update_running_list(Q3ListViewItem *s){

QLabel *name = new QLabel;
QLineEdit *value = new QLineEdit;
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!

high_flyer
10th November 2009, 10:50
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.


//in Controller_run.h
QVector<QLineEdit*> m_vecLineEdit;



void Controller_run::update_running_list(Q3ListViewItem *s){

QLabel *name = new QLabel;
QLineEdit *value = new QLineEdit;
m_vecLineEdit.push_back(value );
.....


in the slot Actualize() you can do something like:


QLineEdit *pLineEdit = sender();
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:


Controller_run::~Controller_run()
{
for(int i=0; i< m_vecLineEdit.size(); i++){
delete m_vecLineEdit[i];
}
}

martisho
10th November 2009, 14:30
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.

danc81
10th November 2009, 14:33
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*

squidge
10th November 2009, 14:51
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.

high_flyer
10th November 2009, 15:39
I agree with fatjuicymole in this case.
QSignalMapper would fit better in here.