Results 1 to 4 of 4

Thread: problem filling a QtableWidget

  1. #1
    Join Date
    Aug 2009
    Posts
    38
    Thanks
    3

    Default problem filling a QtableWidget

    Hi,

    I have a problem filling a qtablewidget. Allways fill the first row. I 've checked the code but I dont see any error. Could you help me please? Many thanks and sorry for my english!

    Qt Code:
    1. ui->tabla->setColumnCount(5);
    2. while ( query4.next() )
    3. {
    4. //QString group = query4.value( 0 ).toString();
    5.  
    6. item->setText(query4.value(0).toString());
    7. ui->tabla->setItem(a,0,item);
    8. item2->setText(query4.value(1).toString());
    9. ui->tabla->setItem(a,1,item2);
    10. item3->setText(query4.value(2).toString());
    11. ui->tabla->setItem(a,2,item3);
    12. item4->setText(query4.value(3).toString());
    13. ui->tabla->setItem(a,3,item4);
    14. //QMessageBox::information(this,"",query4.value(3).toString());
    15. item5->setText(query4.value(4).toString());
    16. ui->tabla->setItem(a,4,item5);
    17. a++;
    18. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2009
    Location
    Finland
    Posts
    63
    Thanks
    1
    Thanked 22 Times in 19 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: problem filling a QtableWidget

    Have you tried to increase the amount of rows in the loop i.e. something like:

    Qt Code:
    1. :
    2. :
    3. item5->setText(query4.value(4).toString());
    4. ui->tabla->setItem(a,4,item5);
    5. a++;
    6.  
    7. ui->tabla->setRowCount(a); // or maybe this needs to be first item in the loop ...
    8. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2007
    Location
    Grenoble, France
    Posts
    80
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem filling a QtableWidget

    The problem is, you're adding five items to QTableWidget and on each iteration changing their contents.
    You can't add the same items several times. If you run your code in console you'll get something like this:
    QTableWidget: cannot insert an item that is already owned by another QTableWidget
    You should rather do something like this:
    Qt Code:
    1. ui->tabla->setColumnCount(5);
    2. while ( query4.next() ) {
    3. for (int i = 0; i < 5; ++i) {
    4. item = new QTableWidgetItem();
    5. item->setText(query4.value(i).toString());
    6. ui->tabla->setItem(a,i,item);
    7. }
    8. ++a;
    9. }
    To copy to clipboard, switch view to plain text mode 
    You have to run a level 3 diagnostic.

    Ashes to ashes, Qt to Qt ( wysota )

  4. The following user says thank you to calhal for this useful post:

    mmm286 (27th May 2010)

  5. #4
    Join Date
    Aug 2009
    Posts
    38
    Thanks
    3

    Default Re: problem filling a QtableWidget

    Quote Originally Posted by calhal View Post
    The problem is, you're adding five items to QTableWidget and on each iteration changing their contents.
    You can't add the same items several times. If you run your code in console you'll get something like this:


    You should rather do something like this:
    Qt Code:
    1. ui->tabla->setColumnCount(5);
    2. while ( query4.next() ) {
    3. for (int i = 0; i < 5; ++i) {
    4. item = new QTableWidgetItem();
    5. item->setText(query4.value(i).toString());
    6. ui->tabla->setItem(a,i,item);
    7. }
    8. ++a;
    9. }
    To copy to clipboard, switch view to plain text mode 
    Many thanks! It works!

Similar Threads

  1. Filling empty cells in QTableWidget
    By lyucs in forum Newbie
    Replies: 2
    Last Post: 20th October 2009, 17:12
  2. Filling a QGraphicsItem
    By c_srikanth1984 in forum Qt Programming
    Replies: 15
    Last Post: 6th July 2009, 14:34
  3. Filling an image
    By Caius Aérobus in forum Qt Programming
    Replies: 6
    Last Post: 6th July 2008, 21:13
  4. Filling combobox from database
    By Philip_Anselmo in forum Qt Programming
    Replies: 3
    Last Post: 11th May 2006, 17:53
  5. filling scrollview
    By illuzioner in forum Qt Programming
    Replies: 1
    Last Post: 17th February 2006, 22:00

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.