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))
{
v.setValue(rul);
return v;
}
…
}
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;
}
…
}
To copy to clipboard, switch view to plain text mode
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");
btRule br;
v.setValue(br);
this->setItemEditorFactory(factory);
editorfactory->registerEditor(v.type(), itemCreator);
}:
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);
}:
To copy to clipboard, switch view to plain text mode
And I am showing the editor in
const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
…
ParamEditor* wdg= new ParamEditor(parent);
//QComboBox* wdg = new QComboBox(parent);
return wdg;
}
QWidget* ModelParamViewer::createEditor(QWidget *parent,
const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
…
ParamEditor* wdg= new ParamEditor(parent);
//QComboBox* wdg = new QComboBox(parent);
return wdg;
}
To copy to clipboard, switch view to plain text mode
My custome editor is:
{
Q_OBJECT
public:
{
QString ss
("QSpinBox{background-color: #ABABAB;border: 1px solid black;}");
setStyleSheet(ss);
setMinimumWidth(200);
setLayout(layout);
layout->addWidget(lbl);
}
};
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);
}
};
To copy to clipboard, switch view to plain text mode
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?
Bookmarks