PDA

View Full Version : Custom item editor is now showing up.



TorAn
3rd April 2010, 02:47
I am trying to create a custom editor for a user datatype to show in a model-view . Here is want I have:

Custom data type btRule (class), registered with Q_DECLARE_METATYPE(btRule)

Model btRulesModel that returns btRule for editing:

QVariant btRulesModel::data(const QModelIndex& index, int role) const
{
btRule& rul = _rules[index.row()];
…
if ( (role == Qt::EditRole) && (index.column() == 1))
{
QVariant v;
v.setValue(rul);
return v;
}
…
}

I have class ModelParamViewer derived from QStyledItemDelegate. In the constructor I am registering custom editor

ModelParamViewer::ModelParamViewer(QObject *parent)
: QStyledItemDelegate(parent)
{
QItemEditorCreator<ParamEditor>* itemCreator =
new QItemEditorCreator<ParamEditor>("btRules");

QVariant v;
btRule br;
v.setValue(br);
QItemEditorFactory *factory = new QItemEditorFactory();
this->setItemEditorFactory(factory);
QItemEditorFactory* editorfactory = this->itemEditorFactory();
editorfactory->registerEditor(v.type(), itemCreator);
}:

And I am showing the editor in

QWidget* ModelParamViewer::createEditor(QWidget *parent,
const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
…
ParamEditor* wdg= new ParamEditor(parent);
//QComboBox* wdg = new QComboBox(parent);
return wdg;
}

My custome editor is:

class ParamEditor : public QWidget
{
Q_OBJECT
public:
ParamEditor (QWidget* parent = 0) : QWidget(parent)
{
QString ss("QSpinBox{background-color: #ABABAB;border: 1px solid black;}");
setStyleSheet(ss);
setMinimumWidth(200);
QHBoxLayout* layout = new QHBoxLayout();
setLayout(layout);
QLabel* lbl = new QLabel"label 1", this);
layout->addWidget(lbl);
}
};
Question:
when I am in the edit mode the editor does not show up. However, when I am replacing editor with ComboBox – combobox shows up as the editor. I understand that ComboBox, just like QLabel, are some of the default editors for the regular datatypes. What am I doing wrong here?

stefanadelbert
15th July 2010, 03:04
I recently used a custom QStyledItemDelegate and custom editor (derived from QWidget). I followed the example Star Delegate (http://doc.qt.nokia.com/4.6.2/itemviews-stardelegate.html) pretty closely. I didn't go down the route of an EditorFactory and by the look of your example it might not be necessary either.