Results 1 to 4 of 4

Thread: QDataWidgetMapper Problem with updating fields

  1. #1
    Join Date
    Jun 2012
    Posts
    33
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default QDataWidgetMapper Problem with updating fields

    Greetings everyone. Once again I require the wisdom of the QT masters.

    I have the following form with a qdatawidgetmapper linked to a table in MySQL.

    Qt Code:
    1. // Setup the model to use in the mapper
    2. model = new QSqlRelationalTableModel(this);
    3. model->setTable("Orders");
    4.  
    5. model->setRelation(7, QSqlRelation("customers", "CustomerNumber", "Name"));
    6. model->setRelation(5, QSqlRelation("employee", "EmployeeNumber", "UserName"));
    7. model->select();
    8. Qt::SortOrder order = Qt::AscendingOrder;
    9. model->sort(0, order);
    10.  
    11. // Setup the mapper for the order widgets
    12. mapper = new QDataWidgetMapper(this);
    13. mapper->setModel(model);
    14. mapper->setItemDelegate(new QSqlRelationalDelegate(model));
    15. mapper->addMapping(ui->idLineEdit, model->fieldIndex("OrderID"));
    16. mapper->addMapping(ui->fullfilledCheckBox, model->fieldIndex("OrderFullfilled"));
    17. mapper->addMapping(ui->fullfilledDateEdit, model->fieldIndex("FullfieldDate"));
    18. mapper->addMapping(ui->orderDateEdit, model->fieldIndex("DateTime"));
    19. mapper->addMapping(ui->dueDateEdit, model->fieldIndex("DueDateTime"));
    20. mapper->addMapping(ui->employeeLineEdit, 5);
    21. mapper->addMapping(ui->dueTimeEdit, model->fieldIndex("DueTime"));
    22. mapper->addMapping(ui->customerLineEdit, 7);
    23. //mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
    To copy to clipboard, switch view to plain text mode 

    Then I have a checkbox which should set the date in the fullfilledDateEdit box to today's date and save it.

    Qt Code:
    1. ui->fullfilledDateEdit->setFocus();
    2. ui->fullfilledDateEdit->setDate(QDate::currentDate());
    3. ui->closePushButton->setFocus();
    4. ui->fullfilledCheckBox->setDisabled(true);
    5. ui->fullfilledCheckBox->setChecked(true);
    6.  
    7. //Save the order
    8. if (saveOrder())
    9. QMessageBox::critical(this,"Update Order Error", "There was an error saving the record!", QMessageBox::Ok);
    To copy to clipboard, switch view to plain text mode 

    SaveOrder simply performs a model->submitall() as shown below.

    Qt Code:
    1. int addOrder::saveOrder()
    2. {
    3. int currIndex = mapper->currentIndex();
    4. if ((ui->customerLineEdit->text()=="") || (ui->employeeLineEdit->text()==""))
    5. return 1; // Notify user that save was not successful
    6.  
    7. // Submit all changes to the database
    8. if(!model->submit() && (!model->lastError().text().contains("No Fields to update"))) { return 1; }
    9. else if (!itemModel->submitAll() && (!itemModel->lastError().text().contains("No Fields to update"))) { return 1; }
    10. else
    11. {
    12. mapper->setCurrentIndex(currIndex);
    13. ui->savePushButton->setEnabled(false);
    14. }
    15.  
    16. return 0;
    17. }
    To copy to clipboard, switch view to plain text mode 

    My problem is that when I press the checkbox the date is changed in the GUI but it does not update the value in the database. When I close the window and open it again it shows the old value.

    Any ideas on why this is not working?

    I tried using manualsubmit but this created other problems in that it would not update anything in the table.

    Thanks in advance,

    Pericles

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QDataWidgetMapper Problem with updating fields

    Do I guess correctly that the setFocus() calls you are doing are intended to trigger the "input finished" behavior of the date time widget?
    I.e.make it look like the user had entered the date and has moved on?

    That might not actually happen, I don't think the Focus event is directly sent to the widget, it is more likely queued for the event loop to process like other events.

    I think the best way would be to use QDataWidgetMapper::currentIndex() plus the column info (either hardcode or through QDataWidgetMapper::mappedSection() and the input widget) and create a QModelIndex for that and call model->setData()
    I.e. changing the actual data, not its UI representation.

    Cheers,
    _

  3. #3
    Join Date
    Jun 2012
    Posts
    33
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QDataWidgetMapper Problem with updating fields

    Thanks for the reply Anda.

    Yes you are right to assume that the setfocus is there to trigger the change in value.

    But I cannot really understand what I need to do to set the data in the model. Could you please provide me with some more information on what I should do?

    I found model->setData() but I do not know what to put in the three fields that it needs. It wants an index, the value (which will be QDate::currentDate()) and an integer that is equal to Qt::EditRole.

    Thanks for the help,

    Pericles

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QDataWidgetMapper Problem with updating fields

    Probably something like this

    Qt Code:
    1. const QModelIndex index = model->index( currIndex, model->model->fieldIndex("FullfieldDate") );
    2. model->setData( index, QDate::currentDate() );
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

Similar Threads

  1. QDataWidgetMapper QSpinBox setFlags problem
    By hnfmr in forum Qt Programming
    Replies: 0
    Last Post: 17th February 2011, 05:55
  2. Replies: 1
    Last Post: 29th January 2011, 13:14
  3. Problem updating a layout
    By Wach in forum Qt Programming
    Replies: 7
    Last Post: 17th August 2010, 00:38
  4. submitAll() not work for updating fields
    By brokensword in forum Qt Programming
    Replies: 1
    Last Post: 7th October 2008, 12:02
  5. QDataWidgetMapper and QSqlRelationalTableModel problem
    By larry104 in forum Qt Programming
    Replies: 1
    Last Post: 14th November 2007, 15:46

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.