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. [solved] QSqlTableModel not showing in QTableView
Forum Updated to NodeBB v4.3 + New Features

[solved] QSqlTableModel not showing in QTableView

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 2.8k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    matobodo
    wrote on last edited by
    #1

    class Sql
    sql.h
    @
    public:
    QSqlTableModel* getCiselnikydata(QString type);
    @

    sql.cpp
    @
    QSqlTableModel* Sql::getCiselnikydata(QString type)
    {
    QSqlTableModel *ciselnikyDataModel = new QSqlTableModel(this,db);
    ciselnikyDataModel->setTable("ciselniky");
    ciselnikyDataModel->select();
    qDebug() << ciselnikyDataModel->lastError().text();
    return ciselnikyDataModel;
    }
    @

    class CiselnikyOkno
    ciselnikyokno.h
    @
    private:
    QSqlTableModel *data;

    private slots:
    void showData(int row);
    @

    ciselnikyokno.cpp
    @
    void CiselnikyOkno::showData(int row)
    {
    if (row >= 0) {
    Sql sql;
    if (sql.connect()) {
    data = sql.getCiselnikydata(ui->type->currentText());
    ui->table->setModel(data);
    }
    }
    }
    @

    The problem is that nothing shows in my QTableView (ui->table)

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

      Hi,

      Maybe a silly question but, are you sure there's something in the table ?

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

      1 Reply Last reply
      0
      • M Offline
        M Offline
        matobodo
        wrote on last edited by
        #3

        Hi,

        i have data in the table and no error shows
        qDebug() << ciselnikyDataModel->lastError().text();

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

          Ok, then also check the row/column count of your model to ensure there's something in it

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

          1 Reply Last reply
          0
          • M Offline
            M Offline
            matobodo
            wrote on last edited by
            #5

            i have added this code
            @
            qDebug() << ciselnikyDataModel->rowCount();
            qDebug() << ciselnikyDataModel->columnCount();
            @

            after the line
            @
            qDebug() << ciselnikyDataModel->lastError().text();
            @

            in sql.cpp. it returns me that i have 10 rows and 4 columns, the same as in the database

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

              Found it:

              @QSqlTableModel *ciselnikyDataModel = new QSqlTableModel(this,db);@

              You are giving your model a parent here however, your Sql object gets destroyed before the end of showData which in turns deletes the model and thus you won't have anything to show.

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

              1 Reply Last reply
              0
              • M Offline
                M Offline
                matobodo
                wrote on last edited by
                #7

                So the solution is to add this line into ciselnikyokno.h
                @
                Sql *sql;
                @

                and this line into ciselniky.cpp
                @
                sql = new Sql(this);
                @

                It works for me, but I have one question.
                Have I done it correct?

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

                  You can simply not set the model parent when constructing it

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

                  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