Results 1 to 6 of 6

Thread: QSqlRelationalTableModel -> Updating model's data

  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; 8th July 2014 at 00:25.

  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: QSqlRelationalTableModel -> Updating model's data

    query() returns a copy of the QSqlQuery object used by the model. Modifying the copy does not affect the original. The same applies to QSqlQuery::record()

    Since QSqlRelationalTableModel already has an implementation for setData(), I would suggest that you do whatever you want to do different and then call the base class implementation.

    Cheers,
    _

  3. #3
    Join Date
    Jul 2014
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSqlRelationalTableModel -> Updating model's data

    Hi,

    first of all, thanks a lot for your answer.

    You said :
    query() returns a copy of the QSqlQuery object used by the model. Modifying the copy does not affect the original. The same applies to QSqlQuery::record()
    Thanks for this, it makes some thinks clearer for me (should have better reed the doc : The QSqlQueryModel class provides a read-only data model for SQL result sets).
    But there is no doubt that there is still something I haven't understood.
    If I want to change the way the data is displayed I need to re-implement the data() function.
    Based on this doc, all I'm able to "create" is that :
    Qt Code:
    1. QVariant EditDataBaseModel::data(const QModelIndex &item, int role) const
    2. {
    3. if (!item.isValid())
    4. return QVariant();
    5. this->query().last();
    6. if (item.row() >= this->query().at())
    7. return QVariant();
    8.  
    9. if (role == Qt::DisplayRole || role == Qt::EditRole)
    10. {
    11. this->query().seek(item.row());
    12. return this->query().record().value(item.column());
    13. }
    14. else
    15. return QVariant();
    16. }
    To copy to clipboard, switch view to plain text mode 
    The problem is that the view isn't updated if the user changes a value (in this project, I'm using the model's default functions for everything except for data()). View isn't updated but model is. If I exit my app and launch it again, I'm then able to view the change I previously made and which have been saved in the DB.
    I probably am totally wrong in the way I want to reach my model's data but I can't figure out how to do it differently.
    I'm using MVCs for the very first time (like a virgin...). I successfully implemented a view, a model and some delegates. Now I want to re-implement some model's functions because I'd like to personalize the way the data is displayed (using the flags, the data and the paint functions). Am I, at least, right on this point ?

  4. #4
    Join Date
    Jul 2014
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSqlRelationalTableModel -> Updating model's data

    Working code :
    Qt Code:
    1. QVariant MyClassName::data(const QModelIndex &item, int role) const
    2. {
    3. if (!item.isValid())
    4. return QVariant();
    5. this->query().last();
    6. if (item.row() >= this->query().at())
    7. return QVariant();
    8.  
    9. if (role == Qt::DisplayRole)
    10. return this->record(item.row()).value(item.column());
    11. else
    12. return QSqlRelationalTableModel::data(item, role);
    13. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    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: QSqlRelationalTableModel -> Updating model's data

    Quote Originally Posted by toma View Post
    Thanks for this, it makes some thinks clearer for me (should have better reed the doc : The QSqlQueryModel class provides a read-only data model for SQL result sets).
    Yes, but you are using a QSqlRelationalTableModel, which is a QSqlTableModel, which is a read/write model.
    See QSqlTableMode::setEditStrategy().

    Quote Originally Posted by toma View Post
    If I want to change the way the data is displayed I need to re-implement the data() function.
    Right. Or using a proxy model.

    Cheers,
    _

  6. #6
    Join Date
    Jul 2014
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QSqlRelationalTableModel -> Updating model's data

    Hi. Once again, thanks for your answer. I'll continue to work on my project.


    See you later.

Similar Threads

  1. Replies: 24
    Last Post: 18th September 2013, 07:35
  2. Replies: 1
    Last Post: 11th July 2012, 23:46
  3. Replies: 7
    Last Post: 22nd November 2009, 20:47
  4. Need help Updating QTreeView model (QAbstractItemModel)
    By iraytrace in forum Qt Programming
    Replies: 1
    Last Post: 26th October 2009, 23:49
  5. Updating QTreeView with custom model
    By supergillis in forum Qt Programming
    Replies: 8
    Last Post: 18th September 2008, 08: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.