PDA

View Full Version : segmentation fault after QTableWidget::sortItems



Lykurg
22nd March 2006, 18:35
Hi,

I fill a QTableWidget in scope:

{
[...]
wdg_check *wdgl_check = new wdg_check();
ui.languages->setCellWidget( currentRow, 2, wdgl_check );
QObject::connect( ui.languages->cellWidget(currentRow,2), SIGNAL(newSelection()), this, SLOT(page1_selection_change()) );
[...]
QTableWidgetItem *newItemC = new QTableWidgetItem( i.value()["ol"]+"-"+i.value()["fl"] );
ui.languages->setItem( currentRow, 6, newItemC);
}
// ui.languages->sortItems( 6, Qt::AscendingOrder );

where ui.languages is a QTableWidget and i.value() a QMap<QString, QString>. wdg_check is a simple Widget with a QCheckbox, which emit the SIGNAL newSelection(), when the state of QCheckbox is changed. And wdg_check provides the function getStatus(), which returns wether the QCheckBox is checked or not (bool).

so I have defined the connection-slot as:

void dgl_newBin::page1_selection_change()
{
for (int i = 0; i < ui.languages->rowCount(); ++i)
{
bool state = static_cast<wdg_check*>(ui.languages->cellWidget(i,2))->getStatus();
}
// (I have all stuf deleted, which doesn't cause the Problem)
}

Ok, if I run the application with that code, all works, but if I uncomment the line "ui.languages->sortItems( 6, Qt::AscendingOrder );" I get an segmentation fault at the line "bool state = static_cast<wdg_check*>(ui.languages->cellWidget(i,2))->getStatus();".

Why? Isn't ui.languages->cellWidget(i,2) valid after sorting?

Thanks,
Lykurg

jpn
22nd March 2006, 19:34
wdg_check is a simple Widget with a QCheckbox, which emit the SIGNAL newSelection(), when the state of QCheckbox is changed. And wdg_check provides the function getStatus(), which returns wether the QCheckBox is checked or not (bool).

If checkbox behaviour is all you need, you could simply use:
QTableWidgetItem::setCheckState() (http://doc.trolltech.com/4.1/qtablewidgetitem.html#setCheckState)
[Edit] All items have a "checkable" flag by default, but as I recall, in case you want a checkbox to appear, you need to initialize those cells to a certain check state with the above method.


I get an segmentation fault at the line "bool state = static_cast<wdg_check*>(ui.languages->cellWidget(i,2))->getStatus();".

Apparently the cast fails. You should always check the pointer after casting.
I'm not sure about the sorting problem, though..

Lykurg
25th March 2006, 06:54
QTableWidgetItem::setCheckState() (http://doc.trolltech.com/4.1/qtablewidgetitem.html#setCheckState)

Ok, than I take the easy way :-) (and now I am able to sort)

Don't why I was geting the segmentation fault, but it dosn't matter anymore, because QTableWidgetItem::setCheckState() works just fine.

Thanks