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);
/*TreeView*/
....
#include <QtGui>
#include "ComboDelegate.h"
.....
/*Error*/ ComboDelegate* delegate = new ComboDelegate();
TreeView->setItemDelegateForColumn(8, delegate);
To copy to clipboard, switch view to plain text mode
/*delegate.h*/
...
{
Q_OBJECT
public:
ComboDelegate
(QObject *parent
= 0);
};
#endif
/*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
To copy to clipboard, switch view to plain text mode
/*delegate.cpp*/
#include "ComboDelegate.h"
{
}
{
list << "a" << "b" << "c" << "d";
editor->addItems(list);
return editor;
}
{
QString value
= index.
model()->data
(index, Qt
::DisplayRole).
toString();
QComboBox *comboBox
= static_cast<QComboBox
*>
(editor
);
comboBox->addItem(value);
}
{
QComboBox *comboBox
= static_cast<QComboBox
*>
(editor
);
QString value
= comboBox
->currentText
();
model->setData(index, value);
}
{
editor->setGeometry(option.rect);
}
/*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);
}
To copy to clipboard, switch view to plain text mode
Any suggestions ??
Bookmarks