PDA

View Full Version : Problems querying on PostgreSQl



jreromero
6th March 2016, 22:21
Dialog_Login.h*******


#include <QDialog>
#include <QtSql>
#include <QSqlDatabase>

class Dialog_Login;
}

class Dialog_Login : public QDialog
{
Q_OBJECT

public:
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 on in QTableWidget, only for testing
But when i running it, the QTableWidget doesn't fill! What's wrong?? I'm Sorry my bad English!! Thanks

jefftee
7th March 2016, 03:25
The query value is a zero based index and your select is for a single column, so you should use query.value(0).toString() to get the first field in the query result. Change that and then verify that you are successfully getting values out of the database before you worry about the QTableWidget.