PDA

View Full Version : auto checkox fresh in qtreeview problem.



yj_yulin
12th July 2010, 11:17
i have a custom model, and want to achieve the effect in attachment pic.
now i can react on the setdata method in model,like below:
//-------------------------------------------------
bool TorgTreeModel::setData( const QModelIndex & index, const QVariant & value, int role /*= Qt::EditRole */ )
{

if (!index.isValid())
return true;


TorgTreeItem *item = static_cast<TorgTreeItem*>(index.internalPointer());

if (role == Qt::CheckStateRole && index.column() ==0)
{
item->setCheckState( static_cast<Qt::CheckState>(value.toUInt()));
// emit dataChanged(createIndex(0,0,m_root_item),
// createIndex(m_root_item->childCount()-1,0,m_root_item));
return true;
}

return QAbstractItemModel::setData(index,value,role);
}

i can change all checkstate of the model data for the right value, but i can't tell qtreeview to refresh the new data.
i use beginresetmodel/endresetmodel ,it does refresh,but qtreeview not keep the display layout.
now with my code, when i switch to other program and back, qtreeview display ok, but this is not the solution.

is there any other convenient way to tell the qtreeview update the display?
or there is other way to achieve the same effect?
thanks in advance!

aamer4yu
12th July 2010, 12:39
Didnt emit dataChanged work for you ?

Also try calling view->update();

yj_yulin
12th July 2010, 14:17
solved by emit dataChanged signal for every model item that change check state.

yj_yulin
13th July 2010, 05:06
oh,it does not solved, there must be something wrong, though i have emit every datachange signal for every change item, qtreeview doesn't refresh well.
may be i don't have write the right code, but it seems beyond my brain now.
i have attach a test demo.

yj_yulin
13th July 2010, 05:09
my compile info: qt 4.6.2 on win xp sp3 with vs2008.
it doesn't use any windows special, it should compile everywhere that qt supported.

yj_yulin
13th July 2010, 05:21
when compile with qt 4.7 beta1, the same effect as 4.6.2.

yj_yulin
13th July 2010, 06:08
seems solved by rewrite method
//-------------------------------------------------
void TorgTreeModel::emitDataChanged( TorgTreeItem* item )
{
TorgTreeItem* parent_item = getOrgItem(item->parent_org_id);
emit dataChanged(createIndex(item->row(),0,parent_item),createIndex(item->row(),1,parent_item));

qDebug() << "datachange" << item->org_full_name;
}