Results 1 to 2 of 2

Thread: Problems querying on PostgreSQl

  1. #1

    Default Problems querying on PostgreSQl

    Dialog_Login.h*******
    Qt Code:
    1. #include <QDialog>
    2. #include <QtSql>
    3. #include <QSqlDatabase>
    4.  
    5. class Dialog_Login;
    6. }
    7.  
    8. class Dialog_Login : public QDialog
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. explicit Dialog_Login(QWidget *parent = 0);
    14. ~Dialog_Login();
    15.  
    16. void Rellenar_Usuarios();
    17. QSqlQueryModel *Modelo1;
    18. }:
    To copy to clipboard, switch view to plain text mode 
    Dialog_Login.cpp*
    Qt Code:
    1. void Dialog_Login::Rellenar_Usuarios()
    2. {
    3.  
    4. db = QSqlDatabase::addDatabase("QPSQL");
    5. db.setDatabaseName("*******");
    6. db.setHostName("********");
    7. db.setUserName("*****");
    8. db.setPassword("*****");
    9. db.setPort(5432);
    10.  
    11.  
    12.  
    13.  
    14.  
    15. if(db.open())
    16.  
    17. {
    18. QSqlQuery query;
    19.  
    20.  
    21.  
    22. query.exec("SELECT id_usuario FROM Usuario");
    23.  
    24. ui->tableWidget->setRowCount(1);
    25.  
    26. ui->tableWidget->setColumnCount(1);
    27. ui->tableWidget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    28.  
    29.  
    30. ui->tableWidget->setHorizontalHeaderLabels(QString("Usuarios").split(";"));
    31.  
    32. int i=0;
    33. while (query.next()) {
    34.  
    35.  
    36. ui->tableWidget->setItem(i,0,new QTableWidgetItem(query.value(1).toString()));
    37.  
    38.  
    39. i++;
    40. ui->tableWidget->insertRow(i);
    41.  
    42. }
    43. }
    44. }
    45.  
    46. void Dialog_Login::on_pushButton_2_clicked()
    47. {
    48. Rellenar_Usuarios();
    49. }
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by anda_skoa; 7th March 2016 at 09:33. Reason: missing [code] tags

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Problems querying on PostgreSQl

    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.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. Tip for DB querying and table output?
    By adutzu89 in forum Newbie
    Replies: 3
    Last Post: 28th January 2014, 12:27
  2. Querying a database and populating a combobox
    By Splatify in forum Newbie
    Replies: 7
    Last Post: 21st February 2011, 11:28
  3. Replies: 2
    Last Post: 11th February 2011, 17:53
  4. Querying object type using itemAt
    By ajb in forum Qt Programming
    Replies: 1
    Last Post: 24th June 2009, 16:35
  5. Querying within Threads
    By bera82 in forum Qt Programming
    Replies: 11
    Last Post: 12th March 2008, 01:08

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.