Strange behavior when set data back from delegate to model



  • Hi everyone!

    In my application, I have a QSqlTableModel filled with the contents of one mysql table.

    I use a QTableView to see the contents and I have subclassed QStyledItemDelegate and implemented an EditDialog to edit the contents using a Dialog. When the user double clicks any row, the Delegate load all selected row's contents into the Dialog and then allows the user to do any modifications.

    To do so, I have reimplemented the following methods from QStyledItemDelegate: createEditor, setEditorData and setModelData.

    Apparently, everything seems to be correct. But when the Dialog exits and everything is being commited (in the middle of setModelData) suddenly the setEditorData is called after calling model->setData(...)!

    When this happens, some content has already been saved and the other part has not been saved. Thus, the delegate reloads information from model and part of the new information is overwritten by the old.

    I will put some code below:

    @QWidget *MedicationDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem& /option/,
    const QModelIndex& /index/) const
    {
    EditMedicationDialog *editor = new EditMedicationDialog(parent);
    editor->setAttribute(Qt::WA_DeleteOnClose, true);
    editor->setModal(true);

    connect(editor, SIGNAL(accepted()), this, SLOT(editorAccepted()));
    connect(editor, SIGNAL(rejected()), this, SLOT(editorRejected()));
    
    return editor;
    

    }@

    @void MedicationDelegate::setEditorData(QWidget *uncastedEditor, const QModelIndex& index) const
    {
    int currentRow = index.row();

    QModelIndex drugClassIndex = index.sibling(currentRow, MedicationModel::DrugClass);
    QModelIndex drugIndex = index.sibling(currentRow, MedicationModel::Drug);
    QModelIndex dosageIndex = index.sibling(currentRow, MedicationModel::Dosage);
    QModelIndex amountIndex = index.sibling(currentRow, MedicationModel::Amount);
    QModelIndex scheduleIndex = index.sibling(currentRow, MedicationModel::Schedule);
    QModelIndex frequencyIndex = index.sibling(currentRow, MedicationModel::Frequency);
    
    QString drugClass = index.model()->data(drugClassIndex, Qt::DisplayRole).toString();
    QString drug = index.model()->data(drugIndex, Qt::DisplayRole).toString();
    QString dosage = index.model()->data(dosageIndex, Qt::DisplayRole).toString();
    QString amount = index.model()->data(amountIndex, Qt::DisplayRole).toString();
    QString schedule = index.model()->data(scheduleIndex, Qt::DisplayRole).toString();
    QString frequency = index.model()->data(frequencyIndex, Qt::DisplayRole).toString();
    
    EditMedicationDialog *editor = qobject_cast<EditMedicationDialog*>(uncastedEditor);
    
    // these methods write in the dialog
    editor->setDrugClass(drugClass);
    editor->setDrug(drug);
    editor->setDosage(dosage);
    editor->setAmount(amount);
    editor->setSchedule(schedule);
    editor->setFrequency(frequency);
    

    }@

    @void MedicationDelegate::setModelData(QWidget *uncastedEditor, QAbstractItemModel *model,
    const QModelIndex& index) const
    {
    int currentRow = index.row();

    QModelIndex drugClassIndex = index.sibling(currentRow, MedicationModel::DrugClass);
    QModelIndex drugIndex = index.sibling(currentRow, MedicationModel::Drug);
    QModelIndex dosageIndex = index.sibling(currentRow, MedicationModel::Dosage);
    QModelIndex amountIndex = index.sibling(currentRow, MedicationModel::Amount);
    QModelIndex scheduleIndex = index.sibling(currentRow, MedicationModel::Schedule);
    QModelIndex frequencyIndex = index.sibling(currentRow, MedicationModel::Frequency);
    
    EditMedicationDialog *editor = qobject_cast<EditMedicationDialog*>(uncastedEditor);
    
    // the editor's methods read info from the dialog
    model->setData(drugClassIndex, editor->drugClass());
    // the problem is in the following line [drugIndex], if I comment it, all will be saved correctly, ignoring this information, obviously.
    model->setData(drugIndex, editor->drug());
    model->setData(dosageIndex, editor->dosage());
    model->setData(amountIndex, editor->amount());
    model->setData(scheduleIndex, editor->schedule());
    model->setData(frequencyIndex, editor->frequency());
    

    }@

    The setData with drugIndex line for some reason calls setEditorData, and when that happens, the consistency is thrown away. Any reason for this to happen? How to fix it? If you need more code I put.

    Sorry about the creepy english.



  • Can anyone help me?



  • Patience is a virtue.
    Please wait at least a couple of days until bumping a topic. It might take some time until someone familiar with the topic reads your post. Thank you. And of course: welcome to DevNet!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.