segmentation fault after QTableWidget::sortItems
Hi,
I fill a QTableWidget in scope:
Code:
{
[...]
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
()) );
[...]
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:
Code:
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
Re: segmentation fault after QTableWidget::sortItems
Quote:
Originally Posted by Lykurg
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()
[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.
Quote:
Originally Posted by Lykurg
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..
Re: segmentation fault after QTableWidget::sortItems
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