Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Need help in QTableView

    General and Desktop
    4
    7
    4409
    Loading More Posts
    • 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.
    • D
      doforumda last edited by

      hi

      I am trying to display data from mysql database table in QTableView. when i run my code it displays this error
      @
      Starting /home/zafar/c++/QSqlTableModel-build-desktop/QSqlTableModel...
      The program has unexpectedly finished.
      /home/zafar/c++/QSqlTableModel-build-desktop/QSqlTableModel exited with code 0
      @

      here is my code
      @
      #include <QtGui/QApplication>
      #include <QtSql>
      #include <QTableView>
      #include "mainwindow.h"

      int main(int argc, char *argv[])
      {
      QApplication a(argc, argv);
      //MainWindow w;

      QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
      db.setDatabaseName("testdb");
      db.setHostName("localhost");
      db.setUserName("root");
      db.setPassword("xxxxxx");
      
      bool ok = db.open();
      
      if(ok) {
          QSqlTableModel *model;
          model->setTable("testTable");
          model->select();
      
          for(int i = 1; i < model->rowCount(); i++) {
              QString fname = model->record(i).value("fname").toString();
              QString lname = model->record(i).value("lname").toString();
              //qDebug() << fname << " " << lname << "\n";
      
              model->setHeaderData(0, Qt::Horizontal, "First Name");
              model->setHeaderData(1, Qt::Horizontal, "Last Name");
      
              QTableView *view = new QTableView;
              view->setModel(model);
              view->show();
          }
      }
      else {
          qDebug() << "Database could not be opened";
      }
      
      //view->show();
      
      return a.exec&#40;&#41;;
      

      }
      @

      how can i solve this problem? Please help

      1 Reply Last reply Reply Quote 0
      • Z
        ZapB last edited by

        You are not creating a QSqlTableModel, only a pointer to it. You need to have

        @QSqlTableModel* model = new QSqlTableModel;@

        Nokia Certified Qt Specialist
        Interested in hearing about Qt related work

        1 Reply Last reply Reply Quote 0
        • D
          Dii last edited by

          Yes, and you don't need to do the setHeaderData(..) so many times, only once, before or after the for{..} stuff.

          1 Reply Last reply Reply Quote 0
          • ?
            Guest last edited by

            I don't think you need the loop at all (at least based on the snip above).

            1 Reply Last reply Reply Quote 0
            • A
              andre last edited by

              The remarks above are true, but I think you'd learn more if you try to learn how to debug your code. If you run the code through a debugger, you would have found that you have a segmentation fault at line 21, indicating the error ZapB points out. QtCreator comes with a debug mode; please learn to use it. It will give you a the stack of function calls that caused the crash. Usually, the culprit is the line closest to the top of your stack that is located in your own code and not in the Qt libs.

              Why are you creating a new table view for every row of your model?

              1 Reply Last reply Reply Quote 0
              • D
                doforumda last edited by

                thanks this problem is solved. now it is working but when i close the window it displays this
                @
                QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
                @

                how to remedy this?

                1 Reply Last reply Reply Quote 0
                • ?
                  Guest last edited by

                  Should you close the db resources before you exit? you can chk further in QSqlDatabase doc

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post