Results 1 to 4 of 4

Thread: QTableWidget with its items stored separately

  1. #1
    Join Date
    Jun 2010
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QTableWidget with its items stored separately

    Hi,
    I have a application using a QTableWidget. Some operations on its items require some time, so I thought, it's attractive to have all these operations done in a seperate class (QThread sublass) and store all QTableWidgetItems in there. I chose to store them in a QVector. If you have better so far, please tell me.
    Somehow, it's a bit difficult to adjust the items in a QVector and connect them with the QTableWidget any time something in the QTableWidget changes. Here's a method in the class that owns the QTableWidget. It should basically just add one row to the QTableWidget, create one new Item for each column in the seperate class, and connect them using QTableWidget::setItem(...).
    Qt Code:
    1. void mainwindow::addRow()
    2. {
    3. ui.tabWidget_xm->setSortingEnabled(false);
    4. ui.tabWidget_xm->setRowCount(ui.tabWidget_xm->rowCount()+1);
    5.  
    6. for( int i=0; i<=ui.tabWidget_xm->columnCount()-1; i++ )
    7. {
    8. xmprocess->tableWidgetItem[i].resize( xmprocess->tableWidgetItem[i].size()+1 );
    9. int rowCount = ui.tabWidget_xm->rowCount();
    10.  
    11. // this one works for some reason...
    12. // for( int i2=0; i2<=rowCount-1; i2++)
    13. // ui.tabWidget_xm->setItem( i2, i, &xmprocess->tableWidgetItem[i][i2] );
    14. ui.tabWidget_xm->setItem( rowCount-1, i, &xmprocess->tableWidgetItem[i][rowCount-1] );
    15. }
    16.  
    17. // some debug messages. no problem so far.
    18. qDebug("tabWidget_xm rowCount: %d", ui.tabWidget_xm->rowCount() );
    19. qDebug("xmprocess->twidgetitem[0].size: %d", xmprocess->tableWidgetItem[0].size() );
    20. qDebug("xmodprocess text: %s", qPrintable(xmprocess->tableWidgetItem[0][ui.tabWidget_xm->rowCount()-2].text()) );
    21.  
    22. // CRASH !!
    23. // access to tabwidget_xm results in segmentation fault.
    24. qDebug("tabWidget_xm text: %s", qPrintable(ui.tabWidget_xm->item( ui.tabWidget_xm->rowCount()-2 ,0)->text()) );
    25. }
    To copy to clipboard, switch view to plain text mode 
    also available here: http://pastebin.com/L5WYXpGX
    Now, this works until the rowCount of the QTableWidget is 16 (it begins at 10). Then, it crashes at the last debug message. I tried it with the section that is commented. The difference here is, this part connects not only the newly created items of the new row, but any item available. afaik, this should not be necessary, as they already got connected before. In fact, I get, as expected, error messages because of this: 'QTableWidget: cannot insert an item that is already owned by another QTableWidget'. Weird thing is, when size is 16 (it would crash with the other code here), this error message suddenly does not appear. Seems to me, that for some reason, exactly at this point, the item loses its owner.
    I'd be grateful for any hints about how I could get this specific method working correctly, but also, if you have any ideas about how to manage the QTableWidget and its Items differently.

  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 with its items stored separately

    Quote Originally Posted by default user View Post
    Hi,
    I have a application using a QTableWidget. Some operations on its items require some time, so I thought, it's attractive to have all these operations done in a seperate class (QThread sublass) and store all QTableWidgetItems in there. I chose to store them in a QVector. If you have better so far, please tell me.
    Did you thought of using your own model in conjunction with a QTableView (or any other class)? Because your whole thing with a custom thread storing your items sounds very strange to me.

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

    default user (13th July 2010)

  4. #3
    Join Date
    Jun 2010
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTableWidget with its items stored separately

    No, didn't think of that. It simplifies the whole thing and this issue with segmentation faults when using addRow() is also obsolete.
    Thanks.

  5. #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 with its items stored separately

    QTableWidget and QTableWidgetItem instances have to be "owned" by the main thread. By the way, you don't store data in threads (unless you use QThreadStorage).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Phonon(video and audio separately)
    By Fastman in forum Qt Programming
    Replies: 3
    Last Post: 1st February 2010, 06:30
  2. QTableWidget->items
    By calireno in forum Newbie
    Replies: 2
    Last Post: 8th October 2009, 18:16
  3. Transfer items from a QTableWidget to another
    By grub87 in forum Qt Programming
    Replies: 1
    Last Post: 17th July 2009, 16:46
  4. Removing items from QtableWidget
    By algajard in forum Qt Programming
    Replies: 1
    Last Post: 17th March 2009, 09:31
  5. adding items in qtablewidget
    By Djony in forum Qt Programming
    Replies: 17
    Last Post: 24th November 2006, 10:03

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.