Results 1 to 9 of 9

Thread: QTableView populating question

  1. #1
    Join Date
    Jul 2008
    Posts
    13
    Thanks
    2

    Default QTableView populating question

    I am populating a QTableView using the following code:

    Qt Code:
    1. int row = ui.booksTable->rowCount();
    2. ui.booksTable->insertRow(row);
    3. item0->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    4. ui.booksTable->setItem(row, 0, item0);
    5.  
    6. item1->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    7. ui.booksTable->setItem(row, 1, item1);
    8.  
    9. item2->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    10. ui.booksTable->setItem(row, 2, item2);
    To copy to clipboard, switch view to plain text mode 

    For some reason, it is adding a spare blank row at the end. This gets messy as each time the button is pressed, the number of blank rows at the end of the table increases. How do I prevent this behaviour?

    Also, I note that after you call clear() or clearContents() on a QTableView, the rowCount() does not reduce back to 0. That seems weird. I mean, shouldn't clear mean clear the table of all the rows and reduce the number of rows to zero?
    Last edited by jpn; 5th August 2008 at 12:19. Reason: missing [code] tags

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView populating question

    this code works perfectly
    Qt Code:
    1. QTableWidget *tw = new QTableWidget(this);
    2. tw->setColumnCount(3);
    3. int row = tw->rowCount();
    4. tw->insertRow(row);
    5. item0->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    6. tw->setItem(row, 0, item0);
    7.  
    8. item1->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    9. tw->setItem(row, 1, item1);
    10.  
    11. item2->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    12. tw->setItem(row, 2, item2);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jul 2008
    Posts
    13
    Thanks
    2

    Default Re: QTableView populating question

    There's something not right somewhere. I'm hitting the function that contains that code twice, and it is adding a 3rd row (an empty row). I cannot explain it.

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView populating question

    ui.booksTable->insertRow(row);

    What if you dont call this ? Is it necessary before setItem() ??

  5. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView populating question

    if you remove
    Qt Code:
    1. insertRow(row)
    To copy to clipboard, switch view to plain text mode 
    then a row isn't created.
    number of rows and columns should be specified before inserting items.

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView populating question

    Quote Originally Posted by onefootswill View Post
    There's something not right somewhere. I'm hitting the function that contains that code twice, and it is adding a 3rd row (an empty row). I cannot explain it.
    can you post code where you create a widget, table and inserting of items in the table?

  7. #7
    Join Date
    Jul 2008
    Posts
    13
    Thanks
    2

    Default Re: QTableView populating question

    Sure,

    Here's the code:

    Qt Code:
    1. void stuffLender::viewBkButtonSlot()
    2. {
    3. tag = BooksView;
    4. setBooksEnv(tag);
    5.  
    6. switch(booksTableHit)
    7. {
    8. case false: populateBooks(); break;
    9. default: break;
    10. }
    11.  
    12. populateBooksTable();
    13. addRowToBooks();
    14. }
    15.  
    16. void stuffLender::populateBooks()
    17. {
    18. QSqlQuery queryBooks;
    19. QSqlQuery queryBookAuthors;
    20. bookList = new QList<book>;
    21. int bkNr = 0;
    22.  
    23. queryBooks.exec("SELECT nr, title, edition FROM books;");
    24. while(queryBooks.next())
    25. {
    26. bkNr = queryBooks.value(0).toInt();
    27. QVector<author> authors;
    28. QString selStmt = "SELECT nr, fname, lname, minitial FROM authors WHERE nr IN (SELECT authorNr FROM bookAuthors WHERE bookNr = ";
    29. selStmt += QString::number(bkNr);
    30. selStmt += ");";
    31.  
    32. queryBookAuthors.exec(selStmt);
    33. while(queryBookAuthors.next())
    34. {
    35. author newAuthor(queryBookAuthors.value(0).toInt(), queryBookAuthors.value(1).toString(), queryBookAuthors.value(2).toString(), queryBookAuthors.value(3).toString());
    36. authors.append(newAuthor);
    37. }
    38.  
    39. book newBk(bkNr, queryBooks.value(1).toString(), queryBooks.value(2).toInt(), authors);
    40. bookList->append(newBk);
    41. }
    42.  
    43. booksTableHit = true;
    44. }
    45.  
    46. void stuffLender::populateBooksTable()
    47. {
    48. ui.booksOnLoan->hide();
    49. ui.booksTable->setRowCount(0);
    50.  
    51. ui.booksTable->show();
    52. ui.booksTable->setHorizontalHeaderLabels(QStringList() << "Title" << "Author/s" << "Ed.");
    53. int nrOfAuthors = 0;
    54.  
    55. for(int row = 0; row < bookList->count(); row++)
    56. {
    57. addRowToBooks();
    58. QString authors;
    59. const QVector<author> *theAuthors = bookList->at(row).getAuthors();
    60. nrOfAuthors = theAuthors->count();
    61. for(int idx = 0; idx < nrOfAuthors; idx++)
    62. {
    63. if(idx == nrOfAuthors - 1)
    64. authors += theAuthors->at(idx).getFname() + " " + theAuthors->at(idx).getLname() + " " + theAuthors->at(idx).getMinitial();
    65. else
    66. authors += theAuthors->at(idx).getFname() + " " + theAuthors->at(idx).getLname() + " " + theAuthors->at(idx).getMinitial() + ", ";
    67. }
    68. ui.booksTable->item(row, 0)->setText(bookList->at(row).getTitle());
    69. ui.booksTable->item(row, 1)->setText(authors);
    70. ui.booksTable->item(row, 2)->setText(QString::number(bookList->at(row).getEdition()));
    71. }
    72. //ui.booksTable->removeRow(ui.booksTable->rowCount());
    73. }
    74.  
    75. void stuffLender::addRowToBooks()
    76. {
    77. int row = ui.booksTable->rowCount();
    78. ui.booksTable->insertRow(row);
    79. item0->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    80. ui.booksTable->setItem(row, 0, item0);
    81.  
    82. item1->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    83. ui.booksTable->setItem(row, 1, item1);
    84.  
    85. item2->setTextAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    86. ui.booksTable->setItem(row, 2, item2);
    87. }
    To copy to clipboard, switch view to plain text mode 

    The addRowToBooks() function is being hit twice (confirmed in debugging). So, I don't know why the 3rd empty row. It makes no sense.

  8. #8
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView populating question

    I think trouble is here:
    Qt Code:
    1. populateBooksTable();
    2. addRowToBooks();
    To copy to clipboard, switch view to plain text mode 
    you insert rows several times in
    Qt Code:
    1. populateBooksTable();
    To copy to clipboard, switch view to plain text mode 
    and then add extra row by calling
    Qt Code:
    1. addRowToBooks();
    To copy to clipboard, switch view to plain text mode 

  9. The following user says thank you to spirit for this useful post:

    onefootswill (7th August 2008)

  10. #9
    Join Date
    Jul 2008
    Posts
    13
    Thanks
    2

    Default Re: QTableView populating question

    Oh my gosh, that was such a dumb mistake. addRowToBooks should never have been called in viewBkButtonSlot at all. It must have been one of those things where I was looking at it so hard that I just could not see what was right there in front of me.

    Thank you very much for your help.

Similar Threads

  1. Updating a data in QTableView question
    By MarkoSan in forum Qt Programming
    Replies: 5
    Last Post: 6th June 2008, 11:45
  2. QTableView and its Scrollbar question
    By MarkoSan in forum Qt Programming
    Replies: 3
    Last Post: 28th May 2008, 14:23
  3. QTableview data display font question
    By MarkoSan in forum Newbie
    Replies: 8
    Last Post: 14th May 2008, 12:57
  4. Question on QAbstractItemModel vs. QTableView
    By lni in forum Qt Programming
    Replies: 1
    Last Post: 26th April 2007, 07:29
  5. QTableView Question
    By kandalf in forum Qt Programming
    Replies: 1
    Last Post: 4th February 2007, 18:02

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.