PDA

View Full Version : QPushButton as editor in QTreeView



folibis
21st January 2014, 00:44
I am working on a project and I need something like a property editor for it.
So I use TreeView for it. I defined my own classes derived from QAbstractItemModel and QStyledItemDelegate. Looks great.

for editing simple values I use QLineEditor`s etc.
But now I need editor for color and here's what I do:
1. I define QPushButton as editor
2. I click on a cell ad my button appears
3. I click on the button, it opens QColorDialog and I select a color.
4. I close color dialog and emit closeEditor and here it crashed! As I understand, when QBushButton(or any else editor) loses focus it removed by TreeView.

Code snippet to show my idea:

QWidget *PropertyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
PropertyItem * item = (PropertyItem *)(index.internalPointer());
if(item)
{
QWidget * editor;
switch(item->GetPropertyType())
{
...
case PropertyItem::PropertyTypeColor:
editor = new QPushButton(parent);
((QPushButton *)editor)->setText("...");
connect(((QPushButton *)editor),SIGNAL(clicked()),this,SLOT(colorTypeCli cked()));
return editor;
break;

default:
return QStyledItemDelegate::createEditor(parent,option,in dex);
}
}
return QStyledItemDelegate::createEditor(parent,option,in dex);
}
void PropertyDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
PropertyItem * item = static_cast<PropertyItem*>(index.internalPointer());
if(item)
{
switch(item->GetPropertyType())
{
...
case PropertyItem::PropertyTypeColor:
currentValue = item->GetValue();
break;
default: QStyledItemDelegate::setEditorData(editor,index);
}
}
}
void PropertyDelegate::colorTypeClicked()
{
QWidget * editor = qobject_cast<QWidget *>(sender());
QColor current = currentValue.value<QColor>();
currentValue = QColorDialog::getColor(current,NULL,tr("Select color"));
editor->setFocus();
emit commitData(editor);
emit closeEditor(editor); // crashed here
}

So my question - how can I keep it from deleting? Or, if my way looks wrong, any good idea to edit color in property editor.

Thanks for advice!

P.S. I've searched the forum, I've googled it but I've not found some usefull code

folibis
24th January 2014, 02:37
OK, I answer to my own post.
I did it with event filter:

inside PropertyDelegate::createEditor() :

...
case PropertyItem::PropertyTypeColor:
editor = new QPushButton(parent);
((QPushButton *)editor)->setText("...");
connect(((QPushButton *)editor),SIGNAL(clicked()),this,SLOT(colorTypeCli cked()));
editor->installEventFilter(const_cast<PropertyDelegate*>(this)); /// this line to install custom event listener
return editor;
break;

and this function to ignore focus losing:

bool PropertyDelegate::eventFilter(QObject *object, QEvent *event)
{
if(event->type() == QEvent::FocusOut)
{
if(QString(object->metaObject()->className()) == "QPushButton") {
return true;
}
}
return QStyledItemDelegate::eventFilter(object,event);
}