Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7520571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:05:39+00:00 2026-05-30T02:05:39+00:00

In my application, I have a QSqlTableModel filled with a table coming from a

  • 0

In my application, I have a QSqlTableModel filled with a table coming from a MySQL connection. To show its contents, I use a QTableView; and to edit and display it, I subclassed QStyledItemDelegate.

I need to have the ability to edit an entire row of the model. To accomplish this, I use the QModelIndex::sibling method in order to retrieve/set all values ​​in that row, regardless of which (row/column) pair “activated” the delegate.

With that, I can build a QDialog subclass (my delegate editor) and fill it with the current values ​​of the selected row. And then let the user edit the content and after commit the changes or discard them, depending on whether the user clicked “save” or “cancel”.

The problem is: I use QAbstractItemModel::setData to save changes back into model. But under certain conditions (still unknown), this method calls QAbstractItemDelegate::setEditorData, which overwrites the new content that is in QDialog subclass for the old content, currently present in the model.

When this happens, any next call to QAbstractItemModel::setData will write the content contained in the model in itself, rather than the new value selected by the user, because the previous call to QAbstractItemDelegate::setEditorData made the QDialog subclass lose all the information chosen by the user, from the column corresponding to the QAbstractItemModel::setData who originated the problem.

Does anyone know how to prevent this problem from occurring?

Some code:

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 sets the current row values in the editor
    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);

    model->setData(drugClassIndex, editor->drugClass());
    model->setData(drugIndex, editor->drug()); // HERE
    model->setData(dosageIndex, editor->dosage());
    model->setData(amountIndex, editor->amount());
    model->setData(scheduleIndex, editor->schedule());
    model->setData(frequencyIndex, editor->frequency());
}

The line with “HERE” comment is causing the problem (according to debug).

EDIT (new info): I just realize that QAbstractItemModel::setData is always called just after the column that caused the “edit” is saved. I always gave two clicks in the “drug” column. Therefore, QAbstractItemDelegate::setEditorData is called after that edition.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:05:40+00:00Added an answer on May 30, 2026 at 2:05 am

    You could use a boolean to prevent the function body to be executed recursively.

    A simpler solution would be

    • to make the model read-only by setting the edit triggers to QAbstractItemView::NoEditTriggers,
    • to use, for example, the signal clicked of the view to open/show the QDialog instead of the delegate,
    • and to associate the controls inside the dialog with the model fields with QDataWidgetMapper.

    The same QDialog could then be reused, and you would populate the controls with QDataWidgetMapper::setCurrentIndex before showing it with exec() and either QDataWidgetMapper::submit() or revert() after the user validates or cancels the changes.

    Since the QDataWidgetMapper and the QTableView would share the same model, the table view would be automatically updated.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My C# application have to read some date from MySQL database. Problem I have
I have application which needs to use a dll (also written by me) which
My application have to load some points from internal sqlite database of android, and
Views in my application have to show a lot of information. So I thought
I have got a requirement from a client. They need three blogging web application
my application have a tabbarcontroller with 4 view Controllers. Its called here : self.window.rootViewController
Users of my application have the possibility of choosing some values from list. The
If an application have to support starting from android 1.6 device and onwards. The
I have Silverlight application that retrieves data from the database through a WCF Service.
i have seen many android database application. Some of the application have its database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.