PDA

View Full Version : Display ItemDelegate Editor always



gast23
5th September 2010, 12:15
Hi,

i wrote a little itemdelegate for my tableview:



#include "checkboxdelegate.h"

#include <QDebug>

CheckboxDelegate::CheckboxDelegate(QObject *parent) :
QStyledItemDelegate(parent)
{
}

QWidget* CheckboxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const {

QCheckBox *editor = new QCheckBox(parent);
return editor;

}

void CheckboxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {

bool value = index.model()->data(index, Qt::EditRole).toBool();

qDebug() << value;

QCheckBox *check = static_cast<QCheckBox*>(editor);
check->setChecked(value);

}

void CheckboxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const {

QCheckBox *check = static_cast<QCheckBox*>(editor);
model->setData(index, check->isChecked(), Qt::EditRole);

qDebug() << "setModelData" << check->isChecked();

}

void CheckboxDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &index) const {

editor->setGeometry(option.rect);

}



Everything works fine. If i click on a cell the editor poups up and i'm able to edit the value.

Now i would like that the editor is always displayed (User sees always the checkbox)

It this possible?

Thanks

waynew
5th September 2010, 14:07
Try using setModelData in your delegate. This should display your object all of the time.

gast23
5th September 2010, 14:25
Sry what do you mean?'

waynew
5th September 2010, 14:53
Here is an example of doing this for a combo box. You should be able to alter this for using a check box.


void ModeDelegate::setModelData(QWidget* editor, QAbstractItemModel* model,
const QModelIndex& index) const
{
QComboBox* comboBox = static_cast<QComboBox*>(editor);
QString value = comboBox->currentText();

model->setData(index, value, Qt::EditRole);
}

void ModeDelegate::updateEditorGeometry(QWidget* editor,
const QStyleOptionViewItem& option, const QModelIndex &/* index */) const
{
editor->setGeometry(option.rect);
}

gast23
5th September 2010, 15:19
Sry but i already have this:



void CheckboxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const {

QCheckBox *check = static_cast<QCheckBox*>(editor);
model->setData(index, check->isChecked(), Qt::EditRole);

qDebug() << "setModelData" << check->isChecked();

}


The checkbox is only displayed when editing...

gast23
5th September 2010, 18:36
Can someone help me?
I would like to display checkboxes always in qtableview :(

tbscope
6th September 2010, 04:33
I guess you need to use a persistent editor?
These need to be set in the view, not the model I think.