PDA

View Full Version : QComboBox in QTreeView



mugi
15th July 2011, 13:15
Hey Everyone,
I have a QTreeView that uses a QAbstractItemModel.
I want Column 8.to use ComboBoxes, so I implemented a new delegate, but when compiling this error comes out: undefined reference to `ComboDelegate::ComboDelegate(QObject*)'

Here is my code:



/*TreeView*/
....
#include <QtGui>
#include "ComboDelegate.h"
.....
/*Error*/ ComboDelegate* delegate = new ComboDelegate();
TreeView->setItemDelegateForColumn(8, delegate);



/*delegate.h*/
...
class ComboDelegate : public QItemDelegate
{
Q_OBJECT
public:

ComboDelegate(QObject *parent = 0);
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;
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
};

#endif



/*delegate.cpp*/
#include "ComboDelegate.h"

ComboDelegate::ComboDelegate(QObject *parent): QItemDelegate(parent)
{
}

QWidget *ComboDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex &/* index */) const
{
QComboBox *editor = new QComboBox(parent);
QStringList list ;
list << "a" << "b" << "c" << "d";
editor->addItems(list);
return editor;
}

void ComboDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QString value = index.model()->data(index, Qt::DisplayRole).toString();
QComboBox *comboBox = static_cast<QComboBox*>(editor);
comboBox->addItem(value);
}

void ComboDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QComboBox *comboBox = static_cast<QComboBox*>(editor);
QString value = comboBox->currentText();
model->setData(index, value);
}

void ComboDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
editor->setGeometry(option.rect);
}

Any suggestions ??

mugi
15th July 2011, 15:30
it works, I just forgot an Include in the .pro.

qtCuckoo
11th April 2012, 10:04
Hi,

I know this thread is almost a year old but could you post a working example of how to load a Combobox into a TreeView?
I'm quite new to the Qt World and I'm trying to do exactly what you did.

Thanks in advance.