Another question about qtreeview...
I created a qtreeview and inserted a checkable item as following:
MyPlot * plot = new MyPlot;
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);
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);
To copy to clipboard, switch view to plain text mode
...and I've connected the model dataChanged signal to the following slot:
{
QVariant variant
= topLeft.
data(Spada
::PlotRole);
if (!variant.canConvert<MyPlot *>())
return;
MyPlot * plot = variant.value<MyPlot *>();
variant = topLeft.data(Qt::ChechStateRole);
{
Qt
::CheckState checked
= (Qt
::CheckState) variant.
convert(QVariant::Int);
if (checked == Qt::Unchecked )
plot->hide();
else
plot->show();
}
}
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();
}
}
To copy to clipboard, switch view to plain text mode
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?
Bookmarks