Results 1 to 2 of 2

Thread: TableView and QDataWidgetMapper

  1. #1
    Join Date
    Aug 2007
    Location
    Ontario, Canada
    Posts
    22
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default TableView and QDataWidgetMapper

    Hi there,

    I seem to be overlooking something linking my TableView data to some other widgets on another form. Everything appears correct, I have followed all the examples I could find but I think I am overlooking something simple and was hoping for some help.

    I want my application to be able to edit the TableView data via the form I have added. The problem I am running into right now is the form is not being mapped properly to the data. I have included a screenshot.



    Am I using the right signal to connect the two? Thanks in advance!

    Qt Code:
    1. createConnection(); //Sqlite DB Connection is made here
    2.  
    3. // Create and sort model for Table View
    4. model = new QSqlRelationalTableModel(ui.tableView);
    5. model->setTable("course");
    6. model->setSort(model->fieldIndex("discipline"), Qt::AscendingOrder);
    7.  
    8. model->setHeaderData(model->fieldIndex("discipline"), Qt::Horizontal, tr("Discipline"));
    9. model->setHeaderData(model->fieldIndex("course"), Qt::Horizontal, tr("Course"));
    10. model->setHeaderData(model->fieldIndex("grade"), Qt::Horizontal, tr("Grade"));
    11. model->setHeaderData(model->fieldIndex("year"), Qt::Horizontal, tr("Year"));
    12. model->setHeaderData(model->fieldIndex("credits"), Qt::Horizontal, tr("Credits"));
    13. model->setHeaderData(model->fieldIndex("semester"), Qt::Horizontal, tr("Semester"));
    14.  
    15. model->select();
    16.  
    17. // Link table view on form to sql table in sqlite db
    18. ui.tableView->setModel(model);
    19. ui.tableView->setColumnHidden(model->fieldIndex("id"), true);
    20. ui.tableView->resizeColumnsToContents();
    21.  
    22.  
    23. void MainWindow::createSignals()
    24. {
    25. connect(ui.tableView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(editEntry(QModelIndex)));
    26. }
    27.  
    28. void MainWindow::editEntry(QModelIndex loc)
    29. {
    30. QSqlRecord record = model->record(ui.tableView->currentIndex().row());
    31. // Create Form to Add/Edit Grades
    32. EntryForm *entryform = new EntryForm(this);
    33.  
    34. // Map widgets to model
    35. mapper = new QDataWidgetMapper(this);
    36. mapper->setModel(model);
    37. mapper->addMapping(entryform->ui.CourseComboBox, model->fieldIndex("discipline"));
    38. mapper->addMapping(entryform->ui.CourseSpinBox, model->fieldIndex("course"));
    39. mapper->addMapping(entryform->ui.YearLineEdit, model->fieldIndex("year"));
    40. mapper->addMapping(entryform->ui.CreditSpinBox, model->fieldIndex("credits"));
    41. mapper->addMapping(entryform->ui.SemesterLineEdit, model->fieldIndex("semester"));
    42.  
    43. // Draw form
    44. entryform->exec();
    45. mapper->setCurrentModelIndex(loc);
    46. mapper->setCurrentIndex(0);
    47.  
    48. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Aug 2007
    Location
    Ontario, Canada
    Posts
    22
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: TableView and QDataWidgetMapper

    I think I fixed it. It is late and I will have another look tomorrow.

    Qt Code:
    1. void MainWindow::editEntry(QModelIndex loc)
    2. {
    3. int selection = ui.tableView->currentIndex().row();
    4. // Create Form to Add/Edit Grades
    5. EntryForm *entryform = new EntryForm(this);
    6.  
    7. // Map widgets to model
    8. mapper = new QDataWidgetMapper(this);
    9. mapper->setModel(model);
    10. mapper->addMapping(entryform->ui.CourseComboBox, model->fieldIndex("discipline"));
    11. mapper->addMapping(entryform->ui.CourseSpinBox, model->fieldIndex("course"));
    12. mapper->addMapping(entryform->ui.YearLineEdit, model->fieldIndex("year"));
    13. mapper->addMapping(entryform->ui.CreditSpinBox, model->fieldIndex("credits"));
    14. mapper->addMapping(entryform->ui.SemesterLineEdit, model->fieldIndex("semester"));
    15.  
    16. mapper->setCurrentModelIndex(loc);
    17. mapper->setCurrentIndex(selection);
    18.  
    19. entryform->exec();
    20. }
    To copy to clipboard, switch view to plain text mode 

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.