Here is my code for my MainWindow:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QSqlDatabase connection = QSqlDatabase::addDatabase("QMYSQL");
connection.setHostName("localhost");
connection.setDatabaseName("dbname");
connection.setUserName("username");
connection.setPassword("Temp");
QTableView *view = new QTableView(this);
view->setMinimumSize(200, 200);
QSqlTableModel *model = new QSqlTableModel(this, connection);
model->setTable("users");
qDebug() << model->columnCount() << model->rowCount();
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
model->removeColumn(0); // don't show the ID
view->setModel(model);
view->show();
this->setWindowTitle("Showing Things");
}
My main.cpp:
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
I know my database contains data, I am basically following the example from the Detailed Explanation here.
This is what I get when I compile and run:

When I expect something like this:

and I do not get it.
What am I doing wrong?
According to the code you posted, you didn’t open the database connection.
QSqlTableModeldoesn’t open it for you.