0

I am having problems using the select function for QSqlTableModel. It always returns false and I have been unable to figure out why.

I have setup a SQL Database using PostgreSQL. I was able to add the database using QSqlDatabase::addDatabase and I have been able to use QSqlQuery to pull data from the database and create new tables and new rows to tables. I am now trying to display the database in a TableView. I found that you can create the QSqlTableModel to plug into the TableView. However, it is failing on the "select" step.

Here is the code I am running:

_database = loadDatabase();
qDebug()<<"table database name: "<<_database.databaseName()<<endl;  //Returns: table database name:  "postgres"
qDebug()<<"table database isOpen: "<<_database.isOpen()<<endl;      //Returns: table database isOpen:  true
qDebug()<<"table database tables: "<<_database.tables()<<endl;      //Returns: table database tables:  ("Test", "testtable")
qDebug()<<"last error: "<<_database.lastError().text()<<endl;       //Returns: last error:  " "

_tableModel = new QSqlTableModel(this,_database);
_tableModel->setTable(_database.tables().at(1));
_tableModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
if(_tableModel->select()){
    qDebug()<<"Table was selected!"<<endl;
}
else{
    qDebug()<<"Table could not be selected"<<endl;
}

qDebug()<<"is database valid: "<<_database.isValid()<<endl;     //Returns: is database valid:  true

"_database" is defined as a QSqlDatabase. It always returns "Table could not be selected".

I found an answer on Stack Overflow that suggested to check whether the database isOpen(), the table name is correct, and if there is any error messages with lastError(). I included those answers in the debug statements in my code. Everything appears to be correct, but yet it returns a false.

Any help would be greatly appreciated. Thanks!

Nekreg45
  • 1
  • 2
  • Does postgres user have permissions to select data? Can you run select query in psql shell? – mugiseyebrows Mar 02 '21 at 21:07
  • I believe so. I am new to this SQL stuff. I am able to use the following code: `code QSqlQuery qry(_database); QString string = "SELECT * FROM " + _database.tables().at(1)+";"; if(qry.exec(string)){ qDebug()<<"Query succeeded!"< – Nekreg45 Mar 03 '21 at 13:53
  • Try adding `qDebug() << _database.lastError().text();` after `_tableModel->select()` to see error message – mugiseyebrows Mar 04 '21 at 09:33
  • It's the same result. It's an empty string. I might try something different than PostgreSQL and see if that has anything to do with it. – Nekreg45 Mar 04 '21 at 12:25

1 Answers1

0

I figured it out. It is a problem with PostgreSQL. Specifically, versions later than 10 or 11 seem to not work with QSqlTableModel. I was using version 13. There was some kind of change in the code that broke this with Qt. Nonetheless, its reported as a Qt bug.

Here is the Qt bug report: https://bugreports.qt.io/browse/QTBUG-79033

It looks like you can use a patch. I ended up reverting mine to an earlier version to fix it.

Thanks for the help!

Nekreg45
  • 1
  • 2