Results 1 to 2 of 2

Thread: QTableWidget - inserting data

  1. #1
    Join Date
    Jul 2010
    Location
    Poland
    Posts
    184
    Thanks
    70
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QTableWidget - inserting data

    Hello!

    I want to show data from my database in QTableWidget. I've wrote somehing like that:

    Qt Code:
    1. bool MainWindow::readFromDatabase()
    2. {
    3.  
    4. [...]
    5.  
    6. ok = bdb.open();
    7.  
    8. if (ok)
    9. {
    10. QSqlQuery query("SELECT * FROM mytable", bdb);
    11.  
    12. while (query.next()) {
    13. row = ui->qtable->rowCount();
    14. ui->qtable->insertRow(row);
    15.  
    16. QTableWidgetItem *item1 = new QTableWidgetItem(query.value(0).toString());
    17. ui->qtable->setItem(row, 0, item1);
    18.  
    19. QTableWidgetItem *item2 = new QTableWidgetItem(query.value(1).toString());
    20. ui->qtable->setItem(row, 1, item2);
    21.  
    22. [...]
    23. }
    24.  
    25. query.clear();
    26. } else {
    27. //out << "database open error!" << endl;
    28. }
    29.  
    30. bdb.close();
    31.  
    32. return ok;
    33. }
    To copy to clipboard, switch view to plain text mode 

    Is it the only way of inserting items into database? Is it necessary to create all these items? Maybe there is way to do it simpler (set position and text which should be there)?

    thanks in advance
    best regards
    Tomasz

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QTableWidget - inserting data

    Use the model view framework. See QSqlTableModel or QSqlQueryModel as well as the introduction of the model view framework in the docs.

  3. The following user says thank you to Lykurg for this useful post:

    Tomasz (30th September 2010)

Similar Threads

  1. Inserting Items in QTableWidget
    By Ma7moud El-Naggar in forum Newbie
    Replies: 1
    Last Post: 30th August 2010, 13:17
  2. Problem while inserting the data into database
    By sudheer168 in forum Qt Programming
    Replies: 2
    Last Post: 13th December 2008, 12:22
  3. Replies: 12
    Last Post: 28th April 2007, 20:25
  4. Problem inserting in QTableWidget
    By DPinLV in forum Qt Programming
    Replies: 2
    Last Post: 2nd August 2006, 00:10

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.