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. Need help in QTableView
QtWS25 Last Chance

Need help in QTableView

Scheduled Pinned Locked Moved General and Desktop
7 Posts 4 Posters 4.8k 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.
  • D Offline
    D Offline
    doforumda
    wrote on last edited by
    #1

    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
    0
    • Z Offline
      Z Offline
      ZapB
      wrote on last edited by
      #2

      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
      0
      • D Offline
        D Offline
        Dii
        wrote on last edited by
        #3

        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
        0
        • ? This user is from outside of this forum
          ? This user is from outside of this forum
          Guest
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            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
            0
            • D Offline
              D Offline
              doforumda
              wrote on last edited by
              #6

              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
              0
              • ? This user is from outside of this forum
                ? This user is from outside of this forum
                Guest
                wrote on last edited by
                #7

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

                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