Results 1 to 3 of 3

Thread: How to use QDataWidgetMapper to manage data is a QSqlTableModel

  1. #1
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default How to use QDataWidgetMapper to manage data is a QSqlTableModel

    Hello!

    Hi have this basic ui to manage data is a MySQL table:
    dialog.png
    The model/view approach works fine.
    I'm trying now a "form" approach, using QDataWidgetMapper with the same model.
    The model:
    Qt Code:
    1. QSqlTableModel *model = new QSqlTableModel(this, QSqlDatabase::database("connection1"));
    2. model->setTable("hobby");
    3. model->setEditStrategy(QSqlTableModel::OnFieldChange);
    4. model->setHeaderData(0, Qt::Horizontal, "ID");
    5. model->setHeaderData(1, Qt::Horizontal, "Nome do passatempo");
    6. model->select();
    7. return model;
    To copy to clipboard, switch view to plain text mode 

    The QDataWidgetMapper:
    Qt Code:
    1. void hobbyDialog::setupWidgetMapper()
    2. {
    3. mapper = new QDataWidgetMapper(this);
    4. mapper->setModel(model);
    5. mapper->addMapping(ui->spinBoxIdHobby, 0);
    6. mapper->addMapping(ui->lineEditNomeHobby, 1);
    7. mapper->toFirst();
    8. ...
    9. }
    To copy to clipboard, switch view to plain text mode 

    Using the tableView, in order no add new record i used:
    Qt Code:
    1. void hobbyDialog::on_inserirUmRegisto_clicked()
    2. {
    3. // Inserir um registo
    4. view->setFocus();
    5. int row = model->rowCount();
    6. model->insertRows(row,1);
    7. QModelIndex index = model->index(row, model->fieldIndex("idHobby"));
    8. view->setCurrentIndex(index);
    9. view->edit(index);
    10. }
    To copy to clipboard, switch view to plain text mode 

    Now, using the form/QDataWidgetMapper, how do i manage the add new record?

  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: How to use QDataWidgetMapper to manage data is a QSqlTableModel

    You code that adds a row only uses the model to do so, right?
    So the same code should still add a row to the model.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    graciano (1st March 2014)

  4. #3
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to use QDataWidgetMapper to manage data is a QSqlTableModel

    My code uses the view:
    Qt Code:
    1. QTableView* hobbyDialog::setupView()
    2. {
    3. view = ui->tableView;
    4. view->setModel(model);
    5. view->setSelectionMode(QAbstractItemView::SingleSelection);
    6. //...
    7. return view;
    8. }
    To copy to clipboard, switch view to plain text mode 

    I tried this and it seems to work!
    Qt Code:
    1. int row = model->rowCount();
    2. model->insertRows(row,1);
    3. mapper->setCurrentIndex(row);
    To copy to clipboard, switch view to plain text mode 
    I confess i was surprised ... in my mind this was supposed to be more complex

Similar Threads

  1. Replies: 2
    Last Post: 15th June 2011, 20:53
  2. Replies: 1
    Last Post: 29th January 2011, 13:14
  3. Replies: 0
    Last Post: 17th October 2010, 12:53
  4. Replies: 3
    Last Post: 4th October 2010, 03:57
  5. How to manage QSqlTableModel database operations?
    By Abk in forum Qt Programming
    Replies: 1
    Last Post: 19th September 2007, 10:44

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.