问题
I am showing you my problem by showing a screen shot. This seems easier to me.
My Table is empty in my program, but it has the right amount of columns and if I add more columns in my sql workbench it will get more rows, but nothing is displayed in them. Here is my Source Code:
void adminUserData::on_pushButton_Load_clicked()
{
// This Object is for connecting to my Database (it works).
Datenbank db;
db.connData();
QSqlQueryModel* model = new QSqlQueryModel();
QSqlQuery* qry = new QSqlQuery(db.db);
qry->prepare("SELECT * FROM worker");
qry->exec();
model->setQuery(*qry);
ui->tableView->setModel(modal);
qDebug() << model->rowCount();
db.discData();
}
I have no clue whats wrong. Here is a screen shot of my database in the workbench:
Thank you guys for helping me. best regards回答1:
Shouldn't "ui->tableView->setModel(modal);" be "ui->tableView->setModel(model);"?
回答2:
I think (since we don't have Datenbank
source code) the problem is in declaring Datenbank db;
on the stack, when the function exist, database is closed leading to invalidating your query and model, to solve the issue, either declare it on the heap using new
or use it as a member of your class.
来源:https://stackoverflow.com/questions/30289813/tableview-in-qt5-doesnt-show-mysql-data-just-empty-rows-are-shown