PDA

View Full Version : QTreeView Checkable Item



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?

norobro
15th September 2011, 18:31
Qt::CheckState checked = (Qt::CheckState) variant.convert(QVariant::Int);
Don't know whether this is your problem or not, but variant.convert() returns a bool. In this case it will always be true (checked in the preceding "if" statement). Try using variant.toInt()

fruzzo
16th September 2011, 10:41
Qt::CheckState checked = (Qt::CheckState) variant.convert(QVariant::Int);
Don't know whether this is your problem or not, but variant.convert() returns a bool. In this case it will always be true (checked in the preceding "if" statement). Try using variant.toInt()

ops...a little careless mistake, I don't realize that the returned type is bool...thanks ;)