Solved Editable QSQLQueryModel behaves different in Qt 5.7 then in Qt 5.4
-
Hello everyone, maybe some one can help with some explaination. Recently I migrated my applications from qt 5.4.1 to Qt 5.7.0. Everything works fine except one thing.
I use a editable Qsqlquerymodel. The implementation is based on the example [http://doc.qt.io/qt-5/qtsql-querymodel-example.html](link url).
In my applications i hide most time some columns, and this seems to make problems in Qt5.7. When I now edit a element in the tableview, the edit works as expected, but the hidden column comes visible again.
here an example how to reproduce
That is how I set the model:editableModel = new EditableSqlModel(this); editableModel->setDBConnectionInfo(hostname, port, databaseName, userName, password, connectionBaseName, sessionWorkUpID); TestView = findChild<QTableView*>("TestView"); TestView->setModel(editableModel); editableModel->refresh(); TestView->hideColumn(0);
The editableModel is the code from the example (to make it easier to reproduce).
Now I know the model emits modelReset(). So I tried connecting on it, to hide the column again. But on a large amount of data this is noticable by the user (as the user can see for a short time the popping up of the column before it gets hidden again).
And the other problem is also all section resize section are going away after editing, so things set like this:
TestView->horizontalHeader()->resizeSection(1, 30); TestView->horizontalHeader()->resizeSection(2, 130); TestView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); TestView->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Fixed);
Here the Problem is that when reacting to modelReset() Signal it seems to early to reset this header stuff, I didn't find the right place.
But I do not understand why this is happening, because in Qt5.4 this code works without the described strange behavior. Maybe I missed something to change.
I would be glad if anyone could give hint how to solve this problems.
-
Hi and welcome to devnet,
Not a direct answer, but do you really need to do a model reset ? Or what is triggering that reset ?
-
@SGaist The modelReset is triggered after setData() was called in the model. This happens with and without set delegates.
-
After a call to setData ? That's pretty unusual.
Why do you need to reset your model ?
-
@SGaist I do not call directly setData() nor resetModel(), it is called by Qt after leaving the editmode. This happens the same in the Qt Example (link posted above), so I assumed that this is normal intended behavior.
Maybe the example of Qt is misleading or incorrect then. But I do not know how to implement it then, because in Qt 5.4 the same code worked as expected (hidden columns didn't pop up unexpected, and resizemodes set on the headerview didn't reset).
-
Ok, that doesn't sound like the correct behavior. Might be a regression.
Can you point the example you are using for that ?
-
@SGaist I used the example http://doc.qt.io/qt-5/qtsql-querymodel-example.html
I just used the files editablesqlmodel.cpp and editablesqlmodel.h from the example, moved the function refresh() to public section and call the class following way:
/* instance of class contained in editablesqlmodel.h/cpp*/ editableModel = new EditableSqlModel(this); /* connecting to my database where a table person is existing with some example data as used in the example*/ editableModel->setDBConnectionInfo(hostname, port, databaseName, userName, password, connectionBaseName, sessionWorkUpID); /* connected to modelReset() just to see when it is called */ connect(editableModel, SIGNAL(modelReset()), this, SLOT(slot_modelResetTest())); /* get the pointer to QTableView instance from my ui file */ TestView = findChild<QTableView*>("TestView"); /* set the model to the view */ TestView->setModel(editableModel); /* initialize the model with the testdata from the database */ editableModel->refresh(); /* hide first column (ID column in database) */ TestView->hideColumn(0);
When now triggered in the editmode in the by double click on a cell in the table slot_modelResetTest() is called after leaving the editmode (by clicking somewhere else).
-
I think I found the problem.
In the implemenation of the example in editablesqlmodel.cpp function setData(), the function clear() is called.
When I comment out the clear() call, everything works as expected. Editing, Insertion, Deletion works now fine, hidden columns stay hidden, and the resizemode of the sections in the header stay as configured at begin.
But I am not sure if this may have some side effects. The documentation of Qt is not very clear in this case to me (Documentation of clear() function in QSqlQueryModel).
Maybe someone can explain it a little bit more to me.
I will test the model some time, if I am satified I will mark this question as solved.
EDIT: @SGaist thanks for helping so much, your right questions leaded to the solution ;-)
-
See QTBUG-49404 for more information about what has changed.