PDA

View Full Version : How to refresh table item



vieraci
31st May 2009, 14:32
Inside the setModelData() method of QItemDelegate I need to reformat the edited text and refresh the table. Which methods are available to do this ?

faldzip
31st May 2009, 15:07
What model do you use? Changes to the model are done by setData() and it should emit dataChanged() signal to notify view about changes. So if you use your own custom model (derived from QAbstractItemModel) make sure you emit dataChanged(). If you use some QStandardItemModel then any change will emit dataChanged() signal so you don't need to do anything, and the view should be refreshed.

vieraci
31st May 2009, 16:30
Model is a QSqlTableModel, maybe the changed signal is being emitted, but I don't know if my method is right.


void LineEditDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QLineEdit *edit = qobject_cast<QLineEdit *>(editor);
QString oldVal = index.model()->data(index, Qt::EditRole).toString();
QString newVal = edit->text();
if (oldVal != newVal)
{
model->setData(index, newVal.remove(QRegExp("[()-]")));
/*
// This is wrong...edit has been destroyed by now.
if ((newVal.left(2) == "04") || (newVal.left(1) == "1"))
edit->setText(newVal.left(2)+"-"+newVal.mid(2,4)+"-"+newVal.mid(6,4));
else
edit->setText("("+newVal.left(2)+")"+newVal.mid(2,4)+"-"+newVal.mid(6,4));
*/
}
}


I'm stripping away the inputMask characters and checking the first 1 or 2 digits to see which format to apply. I save the edited digits in the model but I want the formatted text displayed (e.g. 04-1234-5678)

faldzip
31st May 2009, 17:03
hmm I think I don't understand... You are saving 2 digits in the model but want to display 8 digits with dashes it those cells with 2 digits? so maybe store the whole string in model, or modify the painting in paint() method. Also I would rather use QValidator instead of inputMask.

vieraci
1st June 2009, 00:52
No, I'm saving a phone number, can be between 6 to 10 digits. I'm stripping away the inputMask characters so I can see the left most 1 or 2 digits but I'm saving just the digits.

That was my mistake. I have to save the whole string.

Zuzzu
1st June 2009, 01:43
hmm I think I don't understand... You are saving 2 digits in the model but want to display 8 digits with dashes it those cells with 2 digits? so maybe store the whole string in model, or modify the painting in paint() method. Also I would rather use QValidator instead of inputMask.


Hi,


you have to reimplement the data method for the DisplayRole in order to format "as you want" the saved data. Now, the data method of model is replying with the saved data without the two digits of masks. I think that this is the best way to manage the situation instead reimplementing the paint method.

Bye.

Alessandro