kerim
5th May 2011, 17:38
hello folks,
hope u can help:
the editors returned from my QItemDelegate do not show propperly (e.g. i see just the string "true" rather than the QCheckBox that i returned, except for the case when i double-click my item to edit it, the right widget shows up.
but i want it to be displayed all the time, not when i just editing my table-item.
a little background:
i have an application which uses a QTableView to display some bunch of data.
This data is actually just a list of QRegExp and i want to use QTableView (rows) to be able to modify pattern, pattern-syntax and case-sensitivity (three columns) for each
regex in my data entry.
for this i implemented the following item delegate (hope the amount of following code wont kill somebody):
My ItemDelegate:
// //////////////////////////////////////////
class FilterRecordItemDelegate : public QItemDelegate
// //////////////////////////////////////////
{
//Q_OBJECT
// //////////////////////////////
public:
// //////////////////////////////
FilterRecordItemDelegate(QObject *parent=NULL);
QWidget *createEditor (QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void setEditorData (QWidget *editor, const QModelIndex &index) const;
void setModelData (QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
};
// //////////////////////////////////
FilterRecordItemDelegate::FilterRecordItemDelegate (QObject *parent) : QItemDelegate(parent)
// //////////////////////////////////
{
}
// //////////////////////////////////
QWidget *FilterRecordItemDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
// //////////////////////////////////
{
if (!index.isValid())
return NULL;
QWidget *report = NULL;
int col = index.column();
switch(col)
{
case 1:
{
QComboBox *editor = new QComboBox(parent);
editor->addItem( tr("Regular expression"), QRegExp::RegExp );
editor->addItem( tr("Wildcard") , QRegExp::Wildcard );
editor->addItem( tr("Fixed string") , QRegExp::FixedString );
report = editor;
break;
}
case 2:
{
QCheckBox *editor = new QCheckBox(parent);
report = editor;
break;
}
}
return report;
}
// //////////////////////////////////
void FilterRecordItemDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
// //////////////////////////////////
{
if (!index.isValid())
return;
int col = index.column();
switch(col)
{
case 1:
{
int value = index.model()->data(index, Qt::EditRole).toInt();
QComboBox *cmbBox = static_cast<QComboBox*>(editor);
cmbBox->setCurrentIndex(value);
break;
}
case 2:
{
int value = index.model()->data(index, Qt::EditRole).toBool();
QCheckBox *chckBox = static_cast<QCheckBox*>(editor);
chckBox->setChecked(value);
break;
}
}
}
// //////////////////////////////////
void FilterRecordItemDelegate::setModelData(QWidget *editor,
QAbstractItemModel *model,
const QModelIndex &index) const
// //////////////////////////////////
{
if (!index.isValid())
return;
int col = index.column();
switch(col)
{
case 1:
{
QComboBox *cmbBox = static_cast<QComboBox*>(editor);
int value = cmbBox->currentIndex();
model->setData(index, value, Qt::EditRole);
break;
}
case 2:
{
QCheckBox *chckBox = static_cast<QCheckBox*>(editor);
bool value = chckBox->isChecked();
model->setData(index, value, Qt::EditRole);
break;
}
}
}
what could be the reason that (for example) i do not see a checkbox but just the string "true" for an item for which my delegate returned a QCheckBox ???
Thnx alot.
hope u can help:
the editors returned from my QItemDelegate do not show propperly (e.g. i see just the string "true" rather than the QCheckBox that i returned, except for the case when i double-click my item to edit it, the right widget shows up.
but i want it to be displayed all the time, not when i just editing my table-item.
a little background:
i have an application which uses a QTableView to display some bunch of data.
This data is actually just a list of QRegExp and i want to use QTableView (rows) to be able to modify pattern, pattern-syntax and case-sensitivity (three columns) for each
regex in my data entry.
for this i implemented the following item delegate (hope the amount of following code wont kill somebody):
My ItemDelegate:
// //////////////////////////////////////////
class FilterRecordItemDelegate : public QItemDelegate
// //////////////////////////////////////////
{
//Q_OBJECT
// //////////////////////////////
public:
// //////////////////////////////
FilterRecordItemDelegate(QObject *parent=NULL);
QWidget *createEditor (QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
void setEditorData (QWidget *editor, const QModelIndex &index) const;
void setModelData (QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
};
// //////////////////////////////////
FilterRecordItemDelegate::FilterRecordItemDelegate (QObject *parent) : QItemDelegate(parent)
// //////////////////////////////////
{
}
// //////////////////////////////////
QWidget *FilterRecordItemDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
// //////////////////////////////////
{
if (!index.isValid())
return NULL;
QWidget *report = NULL;
int col = index.column();
switch(col)
{
case 1:
{
QComboBox *editor = new QComboBox(parent);
editor->addItem( tr("Regular expression"), QRegExp::RegExp );
editor->addItem( tr("Wildcard") , QRegExp::Wildcard );
editor->addItem( tr("Fixed string") , QRegExp::FixedString );
report = editor;
break;
}
case 2:
{
QCheckBox *editor = new QCheckBox(parent);
report = editor;
break;
}
}
return report;
}
// //////////////////////////////////
void FilterRecordItemDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
// //////////////////////////////////
{
if (!index.isValid())
return;
int col = index.column();
switch(col)
{
case 1:
{
int value = index.model()->data(index, Qt::EditRole).toInt();
QComboBox *cmbBox = static_cast<QComboBox*>(editor);
cmbBox->setCurrentIndex(value);
break;
}
case 2:
{
int value = index.model()->data(index, Qt::EditRole).toBool();
QCheckBox *chckBox = static_cast<QCheckBox*>(editor);
chckBox->setChecked(value);
break;
}
}
}
// //////////////////////////////////
void FilterRecordItemDelegate::setModelData(QWidget *editor,
QAbstractItemModel *model,
const QModelIndex &index) const
// //////////////////////////////////
{
if (!index.isValid())
return;
int col = index.column();
switch(col)
{
case 1:
{
QComboBox *cmbBox = static_cast<QComboBox*>(editor);
int value = cmbBox->currentIndex();
model->setData(index, value, Qt::EditRole);
break;
}
case 2:
{
QCheckBox *chckBox = static_cast<QCheckBox*>(editor);
bool value = chckBox->isChecked();
model->setData(index, value, Qt::EditRole);
break;
}
}
}
what could be the reason that (for example) i do not see a checkbox but just the string "true" for an item for which my delegate returned a QCheckBox ???
Thnx alot.