TableView in QT5 doesn't show MYSQL Data, just empty rows are shown

三世轮回 提交于 2019-12-24 03:09:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!