Problems querying on Postgres
-
wrote on 6 Mar 2016, 21:02 last edited by JREMIROMERO 3 Jun 2016, 21:15
Dialog_Login.h*******
#include <QDialog>
#include <QtSql>
#include <QSqlDatabase>class Dialog_Login;
}class Dialog_Login : public QDialog
{
Q_OBJECTpublic:
explicit Dialog_Login(QWidget *parent = 0);
~Dialog_Login();void Rellenar_Usuarios();
QSqlQueryModel *Modelo1;
QSqlDatabase db;Dialog_Login.cpp*
void Dialog_Login::Rellenar_Usuarios()
{db = QSqlDatabase::addDatabase("QPSQL"); db.setDatabaseName("*******"); db.setHostName("********"); db.setUserName("*****"); db.setPassword("*****"); db.setPort(5432); if(db.open()) { QSqlQuery query; query.exec("SELECT id_usuario FROM Usuario"); ui->tableWidget->setRowCount(1); ui->tableWidget->setColumnCount(1); ui->tableWidget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding); ui->tableWidget->setHorizontalHeaderLabels(QString("Usuarios").split(";")); int i=0; while (query.next()) { ui->tableWidget->setItem(i,0,new QTableWidgetItem(query.value(1).toString())); i++; ui->tableWidget->insertRow(i); }
}
}void Dialog_Login::on_pushButton_2_clicked()
{
Rellenar_Usuarios();
}I need to see what is in the "id_usuario" field in the user table in the QTableWidget, only for testing
But when i running it, the QTableWidget doesn't fill! What's wrong?? I'm Sorry my bad English!! Thanks -
Hi and welcome to devnet,
The first you should do is print what error you got if
db.open()
fails. Most likely, the driver wasn't loaded. If that's the case, what OS are you running ? -
wrote on 6 Mar 2016, 23:31 last edited by
It does't print any error, I use windows 7, The driver is loaded, i compile it! and when i use the example project sqlbrowser , That shows me the data
-
AFAIK, the sqlbrowser example uses a SQLite database which should always work. Did you modify it to use PostgreSQL ?
In any case, you should also add a check that
query.exec()
successfully ran and again print the error if it failed.How do you know your plugin has been loaded ?
-
It does't print any error, I use windows 7, The driver is loaded, i compile it! and when i use the example project sqlbrowser , That shows me the data
wrote on 7 Mar 2016, 08:18 last edited by
1/5