Results 1 to 7 of 7

Thread: QTableWidget: set items disappear after new insertion

  1. #1
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Exclamation QTableWidget: set items disappear after new insertion

    I have a QTableWidget which has to display a sql result, tipical situation.
    The problem is that in a for cycle, when a row is inserted (composed by 5 items), previouse inserted rows disappear, so the cycle lasts with the table that contains just the last record and the previouse ones are blank... this happen even if I followed the "stardelegate" example...

    Any idea about why it is behaving in such a way?

    This is the incriminated insertion piece:

    (ui.tbwview is the QTableWidget)

    Qt Code:
    1. [...]
    2. ui.tbwview->setRowCount(0);
    3. ui.tbwview->setRowCount(length);
    4. int i=0;
    5. while(query.next())
    6. {
    7. QTableWidgetItem *id=new QTableWidgetItem(query.value(0).toString());
    8. QTableWidgetItem *code=new QTableWidgetItem(query.value(1).toString());
    9. QTableWidgetItem *description=new QTableWidgetItem(query.value(2).toString());
    10. QTableWidgetItem *um=new QTableWidgetItem(query.value(3).toString());
    11. QTableWidgetItem *gm=new QTableWidgetItem(query.value(4).toString());
    12.  
    13. ui.tbwview->setItem(i,0,id);
    14. ui.tbwview->setItem(i,1,code);
    15. ui.tbwview->setItem(i,2,description);
    16. ui.tbwview->setItem(i,3,um);
    17. ui.tbwview->setItem(i,4,gm);
    18. i++;
    19. }
    To copy to clipboard, switch view to plain text mode 
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTableWidget: set items disappear after new insertion

    What does "length" contain? setItem() won't create new rows, it'll substitute the ones already existing, are you aware of that?

  3. #3
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QTableWidget: set items disappear after new insertion

    Quote Originally Posted by wysota View Post
    What does "length" contain? setItem() won't create new rows, it'll substitute the ones already existing, are you aware of that?
    Yes, I am.
    length (sorry, I forgot to explain it) is an int valorized by the query "SELECT COUNT(*) FROM table" and it contains the number of records fetched by the query.
    In substance, I'm trying to create all the required rows and then populate them with setItem(). Isn't it expected to work in this way too? Is there a better way?
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTableWidget: set items disappear after new insertion

    Well... using QSqlQueryModel or QSqlTableModel together with QTableView would be more straightforward.

  5. #5
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QTableWidget: set items disappear after new insertion

    Quote Originally Posted by wysota View Post
    Well... using QSqlQueryModel or QSqlTableModel together with QTableView would be more straightforward.
    Hehe yeah, in fact these seem much easier and usual than my solution...

    anyway I solved it: the QTableWidget had sortingEnabled to true and this somehow messed up every item each time I inserted them... I disabled sorting and now all works as it should.

    Thank you anyway for your help, maybe all this could avoid headaches to someone else
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTableWidget: set items disappear after new insertion

    Quote Originally Posted by Raccoon29 View Post
    anyway I solved it: the QTableWidget had sortingEnabled to true and this somehow messed up every item each time I inserted them... I disabled sorting and now all works as it should.
    See the docs for QTableWidget::setItem:
    Quote Originally Posted by QTableWidget::setItem
    If you want to set several items of a particular row (say, by calling setItem() in a loop), you may want to turn off sorting before doing so, and turn it back on afterwards; this will allow you to use the same row argument for all items in the same row (i.e. setItem() will not move the row).

  7. The following user says thank you to wysota for this useful post:

    mattc (3rd May 2009)

  8. #7
    Join Date
    Sep 2007
    Location
    Sant'Elpidio a Mare, Italy
    Posts
    194
    Thanks
    54
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QTableWidget: set items disappear after new insertion

    Quote Originally Posted by wysota View Post
    See the docs for QTableWidget::setItem:
    Funny: I found it just after I posted here the solution... you can imagine how many laughs...and head hits on the wall...
    --
    raccoon29

    "La mia vita finirà quando non vedrò più la gente ridere...non necessariamente alle mie battute "

Similar Threads

  1. adding items in qtablewidget
    By Djony in forum Qt Programming
    Replies: 17
    Last Post: 24th November 2006, 10:03
  2. insertion of multiple items in a single row
    By grimbaum in forum Qt Programming
    Replies: 4
    Last Post: 17th November 2006, 05:36
  3. QTableWidget row swapping and insertion
    By fritz in forum Qt Programming
    Replies: 4
    Last Post: 17th November 2006, 04:07
  4. Replies: 6
    Last Post: 13th October 2006, 14:40

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.