Results 1 to 6 of 6

Thread: QSqlRelationalTableModel -> Updating model's data

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2014
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QSqlRelationalTableModel -> Updating model's data

    Hi everybody.

    I'm trying to re-implement the setData() function of the QSqlRelationalTableModel class but, as you already guessed it, I'm unsuccessful...
    Already developed an app which uses delegates, overloads functions flags(), data(), rowCount(), columCount()... but as I can't figure out what I'm doing wrong in the setData() function, I made another app with the less code as I could in an attempt to debug it and to share it with you.
    So... here it is (the buggy part is in databasemodel.cpp):


    main.cpp :
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4. MainWindow w;
    5. w.setWindowState(Qt::WindowMaximized);
    6.  
    7. w.show();
    8.  
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 


    mainwindow.cpp :
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent)
    3. {
    4. myTableView = new QTableView(this);
    5. setCentralWidget(myTableView);
    6.  
    7. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    8. db.setDatabaseName("../mydb.db");
    9. db.open();
    10.  
    11. modelConducteurs = new DataBaseModel(this, db);
    12. modelConducteurs->setTable("conducteurs");
    13. modelConducteurs->select();
    14. myTableView->setModel(modelConducteurs);
    15. }
    16.  
    17. MainWindow::~MainWindow()
    18. {
    19. delete myTableView;
    20. delete modelConducteurs;
    21. }
    To copy to clipboard, switch view to plain text mode 


    databasemodel.h :
    Qt Code:
    1. class DataBaseModel : public QSqlRelationalTableModel
    2. {
    3. Q_OBJECT
    4. public:
    5. DataBaseModel(QObject * parent = 0, QSqlDatabase db = QSqlDatabase());
    6. bool setData(const QModelIndex &item, const QVariant &value, int role = Qt::EditRole);
    7. private:
    8. };
    To copy to clipboard, switch view to plain text mode 


    databasemodel.cpp
    :
    Qt Code:
    1. EditDataBaseModel::EditDataBaseModel(QObject *parent, QSqlDatabase db)
    2. {
    3. }
    4.  
    5. bool EditDataBaseModel::setData(const QModelIndex &item, const QVariant &value, int role)
    6. {
    7. this->query().seek(item.row());
    8. switch (role) {
    9. case Qt::EditRole:
    10. qDebug() << value;
    11. this->query().record().setValue(item.column(), value);
    12. qDebug() << this->query().lastError();
    13. qDebug() << this->query().record().value(item.column());
    14. emit dataChanged(item, item);
    15. return true;
    16. break;
    17. default:
    18. break;
    19. }
    20. return false;
    21. }
    To copy to clipboard, switch view to plain text mode 

    The problem is the following : the setValue() function seems to be ineffective :
    - The value of value is correct (displayed thanks to qDebug).
    - qDebug() << this->query().lastError(); returns QSqlError(-1, "", "") (no error).
    - qDebug() << this->query().record().value(item.column()); gives me the unchanged content of the record. As if I had never used setValue() two lines before.


    Did I misunderstood the way I'm supposed to update the model data ?
    Is there another function than setValue that I should be using (using setRecord in setData only leads me to a crashing app) ?


    Thanks in advance for you answers.

    See you.



    Thomas.
    Last edited by toma; 7th July 2014 at 23:25.

Similar Threads

  1. Replies: 24
    Last Post: 18th September 2013, 06:35
  2. Replies: 1
    Last Post: 11th July 2012, 22:46
  3. Replies: 7
    Last Post: 22nd November 2009, 19:47
  4. Need help Updating QTreeView model (QAbstractItemModel)
    By iraytrace in forum Qt Programming
    Replies: 1
    Last Post: 26th October 2009, 22:49
  5. Updating QTreeView with custom model
    By supergillis in forum Qt Programming
    Replies: 8
    Last Post: 18th September 2008, 07:01

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.