Frederik Ubben
Frederik Ubben

Reputation: 143

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

I am showing you my problem by showing a screen shot. This seems easier to me.enter image description here

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: enter image description here Thank you guys for helping me. best regards

Upvotes: 2

Views: 992

Answers (2)

Peter
Peter

Reputation: 16

Shouldn't "ui->tableView->setModel(modal);" be "ui->tableView->setModel(model);"?

Upvotes: 0

ahmed
ahmed

Reputation: 5590

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.

Upvotes: 2

Related Questions