简体   繁体   English

TableView不会将QSqlQueryModel加载到其中

[英]TableView does not load the QSqlQueryModel into it

I'm using a tableView to display some information from a table in my database via a QSqlQueryModel. 我正在使用tableView通过QSqlQueryModel从我的数据库中的表中显示一些信息。 It connects it creates the actual table, it creates the rows and columns and labels them accordingly, when I use model->rowCount(); 它连接它创建实际表,它创建行和列并相应地标记它们,当我使用model->rowCount(); in qDebugg() it shows me the proper number of rows, same for columns. qDebugg()它显示了正确的行数,对于列也是如此。

The problems is my columns on each row where information should be displayed is..... well... empty, BLANK, when i have actual data inside the table and i can't figure it out why and did not find a particular solution on videos, online or on stack overflow (only 1 question that gets close to this but it says to use rowCount() to debug the connection and install missing drivers...) 问题是我的每列上的列应该显示的信息是......好吧......空,BLANK,当我在表格中有实际数据时我无法弄明白为什么并且没有找到特定的视频,在线或堆栈溢出的解决方案(只有一个问题接近这个,但它说使用rowCount()调试连接并安装缺少的驱动程序...)

This is what i tried so far: 这是我到目前为止所尝试的:

QString error=nullptr;
    QSqlDatabase db=this->mDbConnection->getDataBase();
    if(!this->mDbConnection->openDatabase(&error)) {
       QMessageBox::critical(this, "Error: ", error);
       return;
    } else {
        qDebug()<<"Connection to database sucess!";
        db.open();
        QSqlQueryModel *model = new QSqlQueryModel();
       model->setQuery("SELECT [UserID],[FullName],[UserName],[Password],[PermissionID]FROM [Restaurant].[dbo].[Users]");
       ui->tableView->setModel(model);
       qDebug()<<model->rowCount();
       //qDebug()<<data(model->createIndex(0,0));
        db.close();
        //cod aici
    }

//mDbConnection is a custom object databaseconnection.cpp:
bool DatabaseConnection::openDatabase(QString *error) {
    this->db.setDatabaseName(QString("DRIVER={%1};SERVER=%2;DATABASE=%3;UID=%4;PWD=%5;Trusted_Connection=%6;").arg(this->conn->getDriver())
        .arg(this->conn->getServer())
        .arg(this->conn->getDatabaseName())
        .arg(this->conn->getUser())
        .arg(this->conn->getPassword())
        .arg(this->conn->getTrustedConnection() ? "Yes" : "No"));

        if(!this->db.open()) {
            if (error!=nullptr) {
                *error = this->db.lastError().text();
            }
            return false;
        }
        return true;
}

QSqlDatabase DatabaseConnection::getDataBase() {
    return this->db;
}

The result is the table from my database with the labels correct and the good number of rows but with everything empty 结果是我的数据库中的表格,标签正确,行数很多但所有内容都是空的

QSqlDataBase object db needs to be closed in the destructor of the user interface else it won't show up even if you have it loaded in the memory and may behave strange..... So you should use db.open() in the interface constructor and db.close(); QSqlDataBase对象db需要在用户界面的析构函数中关闭,否则即使你将它加载到内存中也不会显示它并且可能表现得很奇怪.....所以你应该使用db.open()接口构造函数和db.close(); in the destructor. 在析构函数中。

DO NOT PUT them both in a function like this: 不要把它们都放在这样的函数中:

void function()
{
db.open()
// code for database here
db.close();
}

it won't work this way and I don't understand yet why 它不会这样工作,我不明白为什么

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM