fruzzo
15th September 2011, 16:51
Another question about qtreeview...
I created a qtreeview and inserted a checkable item as following:
MyPlot * plot = new MyPlot;
QStandardItemModel * model = new QStandardItemModel;
QStandardItem * item = new QStandardItem(plot->title().text());
item->setData( QVariant::fromValue(plot), PlotRole);
item->setSelectable(false);
item->setEditable(false);
item->setCheckable(true);
item->setCheckState(Qt::Checked);
model->appendRow(item);
myTreeView->setModel(model);
...and I've connected the model dataChanged signal to the following slot:
void MyWidget::onDataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight )
{
QVariant variant = topLeft.data(Spada::PlotRole);
if (!variant.canConvert<MyPlot *>())
return;
MyPlot * plot = variant.value<MyPlot *>();
variant = topLeft.data(Qt::ChechStateRole);
if (variant.canConvert(QVariant::Int))
{
Qt::CheckState checked = (Qt::CheckState) variant.convert(QVariant::Int);
if (checked == Qt::Unchecked )
plot->hide();
else
plot->show();
}
}
Now, when I check/uncheck the item checkbox the slot is called but the checked variable is always Qt::PartiallyChecked...why?
Is there an error in the posted code?
I created a qtreeview and inserted a checkable item as following:
MyPlot * plot = new MyPlot;
QStandardItemModel * model = new QStandardItemModel;
QStandardItem * item = new QStandardItem(plot->title().text());
item->setData( QVariant::fromValue(plot), PlotRole);
item->setSelectable(false);
item->setEditable(false);
item->setCheckable(true);
item->setCheckState(Qt::Checked);
model->appendRow(item);
myTreeView->setModel(model);
...and I've connected the model dataChanged signal to the following slot:
void MyWidget::onDataChanged(const QModelIndex & topLeft, const QModelIndex & bottomRight )
{
QVariant variant = topLeft.data(Spada::PlotRole);
if (!variant.canConvert<MyPlot *>())
return;
MyPlot * plot = variant.value<MyPlot *>();
variant = topLeft.data(Qt::ChechStateRole);
if (variant.canConvert(QVariant::Int))
{
Qt::CheckState checked = (Qt::CheckState) variant.convert(QVariant::Int);
if (checked == Qt::Unchecked )
plot->hide();
else
plot->show();
}
}
Now, when I check/uncheck the item checkbox the slot is called but the checked variable is always Qt::PartiallyChecked...why?
Is there an error in the posted code?