PDA

View Full Version : delegate doesn't get called when using QTreeWidget but works find with QTableWidget



torres
21st March 2011, 21:21
The delegate function: createEditor(...) doesn't get called when using a QTreeWidget.
Double clicking on an item in the tree widget has no effect.
Does somebody see what I'm doing wrong?



int main(int argc, char *argv[])
{
QApplication app(argc,argv);
QTreeWidget* tree = new QTreeWidget;

tree->setItemDelegate(new MyDelegate);
tree->setEditTriggers(QAbstractItemView::DoubleClicked | QAbstractItemView::SelectedClicked);

tree->setColumnCount(1);

QTreeWidgetItem *item01 = new QTreeWidgetItem(tree);
item01->setData(0, Qt::EditRole /*Qt::DisplayRole*/, QVariant("item01"));
item01->setText(0, "item01");
tree->addTopLevelItem(item01);

tree->show();
return app.exec();
}


The implementation of the delegate class doesn't matter. The create editor method doesn't get called. If I use a QTableWidget instead of a QTreeWidget, it works fine. The class declaration of the delegate looks like this:



#include <QStyledItemDelegate>

class MyDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
MyDelegate(QObject *parent = 0);
~MyDelegate() {}

void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const;
QSize sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const;
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;

private slots:
void commitAndCloseEditor();
};


Thanks!

wysota
21st March 2011, 21:39
Are the items flagged as editable?

torres
21st March 2011, 22:00
No, that solved the problem:

item01->setFlags(item01->flags() | Qt::ItemIsEditable);

Thanks!