Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableview data disappear..

QTableview data disappear..

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 4 Posters 2.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • TheCipo76T Offline
    TheCipo76T Offline
    TheCipo76
    wrote on last edited by TheCipo76
    #1

    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 pdf

    when 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..

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Are you reseting the database connection ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      TheCipo76T 1 Reply Last reply
      2
      • SGaistS SGaist

        Hi,

        Are you reseting the database connection ?

        TheCipo76T Offline
        TheCipo76T Offline
        TheCipo76
        wrote on last edited by TheCipo76
        #3

        @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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          TheCipo76T 1 Reply Last reply
          1
          • SGaistS SGaist

            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.

            TheCipo76T Offline
            TheCipo76T Offline
            TheCipo76
            wrote on last edited by TheCipo76
            #5

            @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;
            }
            
            
            
            1 Reply Last reply
            0
            • Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @TheCipo76 said in QTableview data disappear..:

              mDatabase.close();

              How should this work? A QSqlModel needs access to the database at any time...

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              TheCipo76T 1 Reply Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                @TheCipo76 said in QTableview data disappear..:

                mDatabase.close();

                How should this work? A QSqlModel needs access to the database at any time...

                TheCipo76T Offline
                TheCipo76T Offline
                TheCipo76
                wrote on last edited by
                #7

                @Christian-Ehrlicher as you see lower..

                0_1545298637152_Schermata 2018-12-20 alle 10.35.51.png

                works!

                1 Reply Last reply
                0
                • Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  It works as long as the model does not need to refetch data from the database (e.g. due to scrolling or other circumstances).

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  TheCipo76T 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    It works as long as the model does not need to refetch data from the database (e.g. due to scrolling or other circumstances).

                    TheCipo76T Offline
                    TheCipo76T Offline
                    TheCipo76
                    wrote on last edited by TheCipo76
                    #9

                    @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

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Online
                      Christian EhrlicherC Online
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      I'm giving up... would you please at least try to not to close the database and see if your problem goes away?

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      TheCipo76T 1 Reply Last reply
                      0
                      • Christian EhrlicherC Christian Ehrlicher

                        I'm giving up... would you please at least try to not to close the database and see if your problem goes away?

                        TheCipo76T Offline
                        TheCipo76T Offline
                        TheCipo76
                        wrote on last edited by TheCipo76
                        #11

                        @Christian-Ehrlicher Yes, i have putted "//" before close database instruction

                        //mDatabase.close();
                        

                        but warning still remain..

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          Are you calling QSqlDatabase::addDatabase("QMYSQL"); in several places in your application ?

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          TheCipo76T 1 Reply Last reply
                          1
                          • SGaistS SGaist

                            Are you calling QSqlDatabase::addDatabase("QMYSQL"); in several places in your application ?

                            TheCipo76T Offline
                            TheCipo76T Offline
                            TheCipo76
                            wrote on last edited by TheCipo76
                            #13

                            @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

                            1 Reply Last reply
                            0
                            • Christian EhrlicherC Online
                              Christian EhrlicherC Online
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              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.

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              TheCipo76T 1 Reply Last reply
                              1
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                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.

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                TheCipo76T 1 Reply Last reply
                                2
                                • SGaistS SGaist

                                  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.

                                  TheCipo76T Offline
                                  TheCipo76T Offline
                                  TheCipo76
                                  wrote on last edited by
                                  #16

                                  @SGaist ok, i create the connection in the main window
                                  and then only open/close database tables in every dialog?

                                  is it right?

                                  1 Reply Last reply
                                  0
                                  • Christian EhrlicherC Christian Ehrlicher

                                    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.

                                    TheCipo76T Offline
                                    TheCipo76T Offline
                                    TheCipo76
                                    wrote on last edited by TheCipo76
                                    #17

                                    @Christian-Ehrlicher i will check all dialogs.. but i think that is what i've done..

                                    jsulmJ 1 Reply Last reply
                                    0
                                    • TheCipo76T TheCipo76

                                      @Christian-Ehrlicher i will check all dialogs.. but i think that is what i've done..

                                      jsulmJ Offline
                                      jsulmJ Offline
                                      jsulm
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

                                      @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.

                                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                                      TheCipo76T 1 Reply Last reply
                                      4
                                      • jsulmJ jsulm

                                        @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.

                                        TheCipo76T Offline
                                        TheCipo76T Offline
                                        TheCipo76
                                        wrote on last edited by
                                        #19

                                        @jsulm Ok, i've modify all my project and all works ok!

                                        Thanks to all for help!

                                        1 Reply Last reply
                                        0

                                        • Login

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • Users
                                        • Groups
                                        • Search
                                        • Get Qt Extensions
                                        • Unsolved