PDA

View Full Version : Checkbox in QTreeView



mugi
8th July 2011, 23:41
Hi,
I need to add checkboxes to the first three columns in a QTreeView which gets its data
from a QAbstractItemModel .
Could some one tell me how to complete my model, so that the user can freely check and uncheck theses checkboxes.



QModelIndex OutputsListModel::index ( int row, int column, const QModelIndex & parent) const
{
return createIndex(row, column);
}
QModelIndex OutputsListModel::parent ( const QModelIndex & index ) const
{
return QModelIndex();
}

int OutputsListModel::rowCount ( const QModelIndex & parent) const
{
return(r);
}
int OutputsListModel::columnCount ( const QModelIndex & parent) const
{
return(c);
}
bool OutputsListModel::hasChildren ( const QModelIndex & parent ) const
{.....
}
QVariant OutputsListModel::data ( const QModelIndex & index, int role) const
{ int i = index.column();
if(role == Qt::DisplayRole)
{.....
}
}
Qt::ItemFlags OutputsListModel::flags(const QModelIndex& index) const
{
if (i == 0 || i== 1 || i == 2)
return Qt::ItemIsUserCheckable | Qt::ItemIsSelectable | Qt::ItemIsEnabled;
return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
}
bool OutputsListModel::setData(const QModelIndex& index, const QVariant& value, int role)
{
return true;
}

Talei
9th July 2011, 00:00
Use QStyledItemDelegate or QAbstractItemDelegate and in paint() paint QCheckbox (), i.e.

QApplication::style()->drawControl( QStyle::CE_CheckBox, &cb_Style, painter );
then create editor QCheckBox if You want save data into model.

Or simply in delegate paint() paint unchecked or checked box depending on the data, and change data not in delegate editor but in QTreeView signal clicked(QModelIndex). Connect that signal to slot and then use

int data = model->data(index, Qt::DisplayRole).toInt();
if( data == 0 )
data = 1;
else
data = 0;

model->setData(data, index );
and delegate will autoamatically update view. So that way You don't create editor.

mugi
9th July 2011, 00:25
thanks a lot for the quick reply
If I don't want to use delegates (actually I don't know how to :(), is there another way to update my view using just my QAbstractItemModel.
Judging from this post it should be possible, the thing is I don't know how to complete the Data and SetData Methods

http://www.qtcentre.org/threads/42139-QTableView-with-User-checkable-checkbox

Talei
9th July 2011, 00:51
IMHO the 2nd way, without editor in delegate is the way You should go.

This will work like this
QTreeView part:
-> user click on index in QTreeView, change model data (signal click(QModelIndex)) - toggle currently stored value from 0 to 1 and from 1 to 0

QAbstractItemDelegate part:
-> paint() paint's checkbox and use that int value to determine what state it should has. setEditorData() (<- You don't need this one when You don't createEditor()) taht way code is simpler for this purpouse IMHO

Model part:
-> setData(), data()

I don't want write here code for You, but what can help You are examples, especially:
model = qtdir/examples/itemviews/editabletreemodel
delegate = qtdir/examples/itemviews/coloreditorfactory (use checkBox, example uses combobox)

mugi
9th July 2011, 01:13
thanks
I will give it a shot ;)