Results 1 to 3 of 3

Thread: Populate QTableWidget

  1. #1
    Join Date
    Jun 2010
    Posts
    100
    Thanks
    13
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Populate QTableWidget

    Hi all!

    I need to populate a tablewidget from a datebase but I can't seem to do it. I have more experience with treewidgets but for this project I want tablewidgets.

    I still don't have much code because I can't do the basic, like add a row..
    The QTableWidget was added directly from the tool box and has 7 Columns and 0 Rows as default.

    I looked up some examples but they didn't work. Is this wrong:

    Qt Code:
    1. QLineEdit *test = new QLineEdit;
    2.  
    3. item->setText("test");
    4.  
    5. int n = ui->tableWidget->rowCount();
    6. ui->tableWidget->setRowCount(n);
    7. ui->tableWidget->setItem(n,0,item);
    8. ui->tableWidget->setCellWidget(n,1,test);
    To copy to clipboard, switch view to plain text mode 

    thanks!
    Last edited by ruben.rodrigues; 3rd May 2011 at 13:27.

  2. #2
    Join Date
    Mar 2010
    Location
    Heredia, Costa Rica
    Posts
    257
    Thanks
    24
    Thanked 17 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Populate QTableWidget

    Hi,
    Modify this line: ui->tableWidget->setRowCount(n+1);
    Add this line after it: ui->tableWidget->setColumnCount(2);

    n at the beginning is 0 (no lines) and you don't have columns. Column or row count will be 1 and each index with be 0.

    Carlos.

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

    ruben.rodrigues (3rd May 2011)

  4. #3
    Join Date
    Sep 2008
    Location
    Poland
    Posts
    80
    Thanks
    4
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Populate QTableWidget

    If you want to display a table from a database,then the better way is to use QTableView as view and QSqlTableModel as model.

Similar Threads

  1. how to populate data in QTableWidget
    By gauravg in forum Qt-based Software
    Replies: 1
    Last Post: 25th March 2011, 12:50
  2. PyQt Populate a QTableWidget
    By n3wcr4zy in forum Newbie
    Replies: 0
    Last Post: 2nd March 2011, 08:20
  3. Replies: 3
    Last Post: 1st February 2011, 11:57
  4. How to populate Combobox in QTableView
    By ronak.vashi in forum Newbie
    Replies: 8
    Last Post: 4th December 2010, 17:09
  5. Using QSettings to populate a QComboBox
    By nbkhwjm in forum Newbie
    Replies: 16
    Last Post: 4th September 2007, 22:34

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.