[Solved] Understanding Model/View programming
-
wrote on 17 Nov 2014, 21:41 last edited by
Hi there
I read "this article":http://qt-project.org/doc/qt-4.8/model-view-programming.html which explained using models, views, delegates and so on. But I don't understand something about delegates and therefore I create this topic.
When user double-clicked on view item the delegate for this item creates an editor and then he is placed in item rect. But I don't understand when invokes the setModelData() function and when the editor closed after editing.
Is it happens when user press Enter button or click on another cell(in my case I use QTableView) or at another time?Thank you in advance.
-
wrote on 20 Nov 2014, 22:20 last edited by
Hi,
I am apologize if put it is not clear. I mean next.
This is my code
@void FloatDelegate::setModelData(QWidget *editor, QAbstractItemModel model, const QModelIndex &index) const {
QVariant text = (static_cast<QLineEdit>(editor))->text();if (model->setData(index, text, Qt::EditRole)) { closeEditor(editor); } // show tooltip else { }
}@
There I call model's function setData(). In case of valid data emits signal dataChanged() and function return true.
@bool CriteriaTableModel::setData(const QModelIndex &index, const QVariant &value, int role) {
if (!index.isValid()) {
return false;
}if ( mSM->suggestData(mParams.value( index.column() ), value) ) { emit dataChanged(index, index); return true; } else { return false; }
}@
mSM - object of class that contains app logic, don't pay attention to it.
If setData() responses true so I emit signal closeEditor() otherwise I want show to user a tooltip which prompts him what going wrong.
So I want ask if I do all properly? -
AFAIK, setModelData is not the place for validation, you should have that part in your editor logic.
Also, note that your model setData doesn't do anything related to the data your are giving it so you won't have anything saved.
-
wrote on 23 Nov 2014, 14:20 last edited by
[quote author="SGaist" date="1416698916"]so you won't have anything saved.[/quote]
Hi
What this phrase means? I have to save editor data in setModelData() of delegate?
In my app I need do some verification of data entered by user, therefore i invoke suggestData().
If I don't must verify in the setModelData() where I can do it? -
That phrase mean that your implementation of CriteriaTableModel::setData doesn't save anything in your model unless suggestData store something, does it ?
The idea of setModelData is to take your data from your editor and put them in the model. However it's not the place for validation, that's your editor job.
-
wrote on 24 Nov 2014, 21:57 last edited by
But not always editor can verify data. I explain.
One of the columns consists of strings that must be unique. I check if entered by user string is really unique by mSM->suggestData(). Or you propose to perform validation when user changes the data of editor?
Now if string is already exists I can't add it to underlying data structure.
Now my CriteriaTableModel::setData() looks like this
@bool CriteriaTableModel::setData(const QModelIndex &index, const QVariant &value, int role) {
if (!index.isValid() || role != Qt::EditRole) {
return false;
}if ( _sm->setCriterionData( mParams.value(index.column()), value.toString() ) ) { if (index.column() == mNameItemColumn) { int row = _rows.values().indexOf(mSelectedCriterionName); mSelectedCriterionName = index.data(Qt::EditRole).toString(); _rows.insert(row, mSelectedCriterionName); } emit dataChanged(index, index); return true; } else { return false; }
}@
I stored only data from the mNameItemColumn in my model. _rows is a QHash which is an internal model data. -
You can either give your editor access to your model to ensure no invalid input is given or give it a list of invalid input based on your model content.
-
You're welcome !
If this answers your question, then please update the thread title prepending [solved] so other forum users may know a solution has been found :)
4/8