QTableview data disappear..
-
Hi,
in my project i have 1 main form
from main i open 2 dialog at the same time:first dialog (modeless) show data from mysql database in a tableview (QSqlTableModel)
the second (modal) contain a Qtableview (QStandarItemModel and some QSqlQuery) that allow to generate pdfwhen i open the first dialog the data was correctly showed
but when i open the second dialog and then click on the first dialog the tableview (of the first dialog) was cleared and the data disappear..both the dialog works on the same database but different tables
i don't understand why..
any ideas?
I can post the code but is quite long..
-
Hi,
Are you reseting the database connection ?
-
@SGaist no but i have found in "Application Output":
QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.i've put DB declaration and DB.close() within brakets, than out of brakets, called:
QSqlDatabase::removeDatabase("QMYSQL");
but warning still remain..
Note that i can't use:
QSqlDatabase db = QSqlDatabase::database("dbname"); --> give me this error: Driver not loaded
but i'm using
QSqlDatabase mDatabase = QSqlDatabase::addDatabase("dbname"); --> run correctly
-
Can you show exactly how you are handling QSqlDatabase in your application ?
Because it looks like you are either keeping local objects of that type or adding and removing the connection in several different unsynchronised parts of your application.
-
Can you show exactly how you are handling QSqlDatabase in your application ?
Because it looks like you are either keeping local objects of that type or adding and removing the connection in several different unsynchronised parts of your application.
@SGaist this is a dialog who show data from DB in a QTableView
#include "statoordini.h" #include "ui_statoordini.h" #include "QtSql" #include "QtDebug" //#include <QSqlTableModel> #include <QSqlError> #include <QMessageBox> StatoOrdini::StatoOrdini(QWidget *parent, const QString &Utente, const QString &Password) : QDialog(parent), ui(new Ui::StatoOrdini), Utente(Utente), Password(Password) { ui->setupUi(this); //qputenv("QT_DEBUG_PLUGINS", QByteArray("1")); { QSqlDatabase mDatabase = QSqlDatabase::addDatabase("QMYSQL"); //localhost mDatabase.setHostName("192.168.1.250"); //3306 mDatabase.setPort(3307); mDatabase.setDatabaseName("MISSORA"); mDatabase.setUserName(Utente); mDatabase.setPassword(Password); if (!mDatabase.open()) { QMessageBox::critical(this, "Error", mDatabase.lastError().text()); return; }; QSqlTableModel *mModel = new QSqlTableModel(this); mModel->setTable("ORDINI"); mModel->select(); ui->tableView->setModel(mModel); // Ordina al click su Header ui->tableView->setSortingEnabled(true); ui->tableView->horizontalHeader()->setSectionsClickable(1); // Ridimensiona Colonne tableView al Contenuto ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents); ui->tableView->setColumnHidden(0,true); ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers); ui->tableView->show(); QString Name = mDatabase.connectionName(); QMessageBox msg; msg.setText(Name); msg.exec(); mDatabase.close(); } QSqlDatabase::removeDatabase("QMYSQL"); } StatoOrdini::~StatoOrdini() { //qt_sql_default_connection; //QSqlDatabase::removeDatabase("QMYSQL"); delete ui; }
-
@TheCipo76 said in QTableview data disappear..:
mDatabase.close();
How should this work? A QSqlModel needs access to the database at any time...
-
@TheCipo76 said in QTableview data disappear..:
mDatabase.close();
How should this work? A QSqlModel needs access to the database at any time...
-
It works as long as the model does not need to refetch data from the database (e.g. due to scrolling or other circumstances).
-
It works as long as the model does not need to refetch data from the database (e.g. due to scrolling or other circumstances).
@Christian-Ehrlicher No, i'have tried to scroll and works fine.
This is only view of DB data.. no need to modify nothing in this dialog
and no need to reload data -
I'm giving up... would you please at least try to not to close the database and see if your problem goes away?
-
I'm giving up... would you please at least try to not to close the database and see if your problem goes away?
@Christian-Ehrlicher Yes, i have putted "//" before close database instruction
//mDatabase.close();
but warning still remain..
-
Are you calling
QSqlDatabase::addDatabase("QMYSQL");
in several places in your application ? -
Are you calling
QSqlDatabase::addDatabase("QMYSQL");
in several places in your application ?@SGaist Yes, in every dialog who need to connect to database..
but in every dialog call
ordforauto::~ordforauto() { if (aDatabase.open()) { aDatabase.close(); } QSqlDatabase::removeDatabase("QMYSQL"); delete ui; }
in the code or at least in the unload istructions as you see
-
http://doc.qt.io/qt-5/qsqlquery.html#details
Warning: You must load the SQL driver and open the connection before a QSqlQuery is created. Also, the connection must remain open while the query exists; otherwise, the behavior of QSqlQuery is undefined.
-
Why are you doing it like that ? There's no need to nuke and recreate the connection each time.
If you really want to do it like that, then you should create a connection with a different name in each of your dialog.
Because currently, you are manipulating the default connection from several places in a wrong manner.
-
Why are you doing it like that ? There's no need to nuke and recreate the connection each time.
If you really want to do it like that, then you should create a connection with a different name in each of your dialog.
Because currently, you are manipulating the default connection from several places in a wrong manner.
-
http://doc.qt.io/qt-5/qsqlquery.html#details
Warning: You must load the SQL driver and open the connection before a QSqlQuery is created. Also, the connection must remain open while the query exists; otherwise, the behavior of QSqlQuery is undefined.
@Christian-Ehrlicher i will check all dialogs.. but i think that is what i've done..
-
@Christian-Ehrlicher i will check all dialogs.. but i think that is what i've done..
@TheCipo76 As shown here: http://doc.qt.io/qt-5/qsqldatabase.html
Create and open db connection:QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL"); db.setHostName("acidalia"); db.setDatabaseName("customdb"); db.setUserName("mojito"); db.setPassword("J0a1m8"); bool ok = db.open();
When you need the connection just get it:
QSqlDatabase db = QSqlDatabase::database();
No need to open and close it all the time.
-
@TheCipo76 As shown here: http://doc.qt.io/qt-5/qsqldatabase.html
Create and open db connection:QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL"); db.setHostName("acidalia"); db.setDatabaseName("customdb"); db.setUserName("mojito"); db.setPassword("J0a1m8"); bool ok = db.open();
When you need the connection just get it:
QSqlDatabase db = QSqlDatabase::database();
No need to open and close it all the time.