PDA

View Full Version : editors returned from QItemDelegate do not show propperly



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.

kerim
6th May 2011, 09:25
ok, maybe my question was not clear:

let me tell my problem in a different way:

how do i put a (lets say) QComboBox or QCheckBox into a QTableView and view these Widgets also when no editing is done, means: i dont want the table to contain entries like "true" or "false", but show the QCheckBox.

what am i doing wrong?
please any hints or examples are much appreciated.

thnx.

Added after 1 40 minutes:

some additional infos for my problem:

i am using a custom model that manages a list of QRegExp. the table-view contains each of the QRegExp instances in rows and the three columns contain the properties
pattern, patternsyntax and case-sensitivity of each QRegExp instance.

my delegate shall display first columns as QStrings, second columns as QComboBox and third as QCheckBox.
this works fine only for editing data (when double clicking items in table) but items are not displayed propperly (means: i see "true" or "false" entries rather than a QCheckBox).

i think problem is with my custom models data:


// //////////////////////////////////////////////
QVariant FilterRecordModel::data(const QModelIndex &index,
int role) const
// //////////////////////////////////////////////
{
if (!index.isValid())
return QVariant();

if ((index.row() >= (int)filterRecordLst.size()) ||
(index.row() < 0) ||
filterRecordLst.isEmpty() )
return QVariant();

QVariant report;
if ( role == Qt::DisplayRole || role == Qt::EditRole )
{
int row = index.row();
int col = index.column();
QRegExp rexp = filterRecordLst[row];
switch(col)
{
case 0:
{
report = QVariant(rexp.pattern());
break;
}
case 1:
{
int val = -1;
switch( rexp.patternSyntax() )
{
case QRegExp::RegExp: {val=0;break;}
case QRegExp::Wildcard: {val=1;break;}
case QRegExp::FixedString: {val=2;break;}
default:break;
}
report = QVariant(val);
break;
}
case 2:
{
bool cs = (rexp.caseSensitivity()==Qt::CaseSensitive);
report = QVariant(cs);
break;
}
}
}
return report;
}