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 doesn't show data.
Qt 6.11 is out! See what's new in the release blog

QTableView doesn't show data.

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 1.1k 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.
  • V Offline
    V Offline
    Vladislav Lisovenko
    wrote on last edited by
    #1

    I'm trying to display data from database. There is my code:

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        DBClient dbClient;
        QSqlDatabase *db=dbClient.getConnection();
    
        QSqlQueryModel model;
        model.setQuery("select _id,descr from students limit 10");
    
        if(model.lastError().isValid()){
            qDebug() << model.lastError();
        }
    
        ui->tableView->setModel(&model);
    
        qDebug() << ui->tableView->model()->columnCount();
        qDebug() << ui->tableView->model()->rowCount();
    }
    

    Application output:
    2
    10
    It means that columns and data exist in tableView, but visually tableView is absolutely empty, I don't see neither columns nor data and I don't know why.

    Where there's a will, there is a way.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      Devopia53
      wrote on last edited by
      #2

      Hi

      Since you allocate your QSqlQueryModel on the stack it's destroyed at the end of MainWindow constructor.

      So you should allocated on the heap...

      V 1 Reply Last reply
      1
      • D Devopia53

        Hi

        Since you allocate your QSqlQueryModel on the stack it's destroyed at the end of MainWindow constructor.

        So you should allocated on the heap...

        V Offline
        V Offline
        Vladislav Lisovenko
        wrote on last edited by
        #3

        @Devopia53 Hi, it works, thanks.

        Where there's a will, there is a way.

        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