Solved QTableView doesn't show data.
-
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. -
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...
-
@Devopia53 Hi, it works, thanks.