PDA

View Full Version : QTableWidget with its items stored separately



default user
13th July 2010, 13:54
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(...).

void mainwindow::addRow()
{
ui.tabWidget_xm->setSortingEnabled(false);
ui.tabWidget_xm->setRowCount(ui.tabWidget_xm->rowCount()+1);

for( int i=0; i<=ui.tabWidget_xm->columnCount()-1; i++ )
{
xmprocess->tableWidgetItem[i].resize( xmprocess->tableWidgetItem[i].size()+1 );
int rowCount = ui.tabWidget_xm->rowCount();

// this one works for some reason...
// for( int i2=0; i2<=rowCount-1; i2++)
// ui.tabWidget_xm->setItem( i2, i, &xmprocess->tableWidgetItem[i][i2] );
ui.tabWidget_xm->setItem( rowCount-1, i, &xmprocess->tableWidgetItem[i][rowCount-1] );
}

// some debug messages. no problem so far.
qDebug("tabWidget_xm rowCount: %d", ui.tabWidget_xm->rowCount() );
qDebug("xmprocess->twidgetitem[0].size: %d", xmprocess->tableWidgetItem[0].size() );
qDebug("xmodprocess text: %s", qPrintable(xmprocess->tableWidgetItem[0][ui.tabWidget_xm->rowCount()-2].text()) );

// CRASH !!
// access to tabwidget_xm results in segmentation fault.
qDebug("tabWidget_xm text: %s", qPrintable(ui.tabWidget_xm->item( ui.tabWidget_xm->rowCount()-2 ,0)->text()) );
}
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.

Lykurg
13th July 2010, 14:13
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.

default user
13th July 2010, 20:49
No, didn't think of that. It simplifies the whole thing and this issue with segmentation faults when using addRow() is also obsolete.
Thanks.

wysota
13th July 2010, 22:46
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).