Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Strange behavior when set data back from delegate to model
Qt 6.11 is out! See what's new in the release blog

Strange behavior when set data back from delegate to model

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 2.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Can anyone help me?

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        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!

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved