Unsolved Signal returnPressed() doesn't work in setModelData
-
Hi,
I'm trying to use signal returnPressed() in setModelData:void myDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { QLineEdit *edit = qobject_cast<QLineEdit*>(editor); if(edit) { model->setData (index,edit->text ()); QVariant f(index.data(Qt::DisplayRole)); QString modified; modified = f.toString (); qDebug() << "Modified: " << modified; connect(edit,SIGNAL(returnPressed()), this,SLOT(saveChangedItem())); return; } }
Everything works fine but when the connect processed nothing happens (it does not go to saveChangedItem(). Please point me to the right direction to make this work. Thank you.
-
@gabor53 Are you aware that connect() does not call the slot? It just connect the signal to the slot. The slot is called when the signal appears. That means
connect(edit,SIGNAL(returnPressed()), this,SLOT(saveChangedItem()));
will not call saveChangedItem().
saveChangedItem() will be called as soon as returnPressed() is emitted in edit.
So, is the signal emitted?
To be sure connect() succeeded you should print out its return value:qDebug() << connect(edit,SIGNAL(returnPressed()), this,SLOT(saveChangedItem()));
-
Hi,
Provide the debug statement and check the return value, to ensure the signal is emitted or not,
and connect() statements establish connection between two objects, and will not call slot until the signal of one object is emitted.bool value = << connect(edit,SIGNAL(returnPressed()), this,SLOT(saveChangedItem())); qDebug () << value << endl;
Sample Program : to ensure signal is emitted or not.
m_edit = new QLineEdit; m_label = new QLabel value = connect(m_edit,SIGNAL(returnPressed()),this,SLOT(getData()),Qt::UniqueConnection);
void LineEDitSignal::getData()
{
qDebug () << value;m_label->setText(m_edit->text()); qDebug () << "lineedit value :" << m_edit->text() << endl;
}
Thanks,