I copied qt designers' SheetDelegate class with some modification to show sheets like what's in designers' widgetbox. But the compiler says "StencilView.cpp:21: undefined reference to `SheetDelegate::SheetDelegate(QObject*)' ".

Modified SheetDelegate.h
Qt Code:
  1. #ifndef SHEET_DELEGATE_H
  2. #define SHEET_DELEGATE_H
  3.  
  4. #include <QStyledItemDelegate>
  5.  
  6. class SheetDelegate: public QStyledItemDelegate
  7. {
  8. Q_OBJECT
  9. public:
  10. SheetDelegate(QObject *parent = 0);
  11.  
  12. virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
  13. virtual QSize sizeHint(const QStyleOptionViewItem &opt, const QModelIndex &index) const;
  14. };
  15.  
  16. #endif // SHEET_DELEGATE_H
To copy to clipboard, switch view to plain text mode 

Modified SheetDelegate.cpp
Qt Code:
  1. #include <QtGui/QStyle>
  2. #include <QtGui/QPainter>
  3.  
  4.  
  5. SheetDelegate::SheetDelegate(QObject *parent)
  6. : QStyledItemDelegate(parent)
  7. {
  8. }
  9.  
  10. void SheetDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
  11. {
  12. if (index.model()->parent(index).isValid()) {
  13. // this is a top-level item.
  14. QStyleOptionButton buttonOption;
  15.  
  16. buttonOption.state = option.state;
  17. #ifdef Q_WS_MAC
  18. buttonOption.state |= QStyle::State_Raised;
  19. #endif
  20. buttonOption.state &= ~QStyle::State_HasFocus;
  21.  
  22. buttonOption.rect = option.rect;
  23. buttonOption.palette = option.palette;
  24. buttonOption.features = QStyleOptionButton::None;
  25. QStyle::drawControl(QStyle::CE_PushButton, &buttonOption, painter, parent);
  26.  
  27. QStyleOption branchOption;
  28. static const int i = 9; // ### hardcoded in qcommonstyle.cpp
  29. QRect r = option.rect;
  30. branchOption.rect = QRect(r.left() + i/2, r.top() + (r.height() - i)/2, i, i);
  31. branchOption.palette = option.palette;
  32. branchOption.state = QStyle::State_Children;
  33.  
  34. if (parent->isExpanded(index))
  35. branchOption.state |= QStyle::State_Open;
  36.  
  37. QStyle::drawPrimitive(QStyle::PE_IndicatorBranch, &branchOption, painter, parent);
  38.  
  39. // draw text
  40. QRect textrect = QRect(r.left() + i*2, r.top(), r.width() - ((5*i)/2), r.height());
  41. QString text = elidedText(option.fontMetrics, textrect.width(), Qt::ElideMiddle,
  42. index.model()->data(index, Qt::DisplayRole).toString());
  43. QStyle::drawItemText(painter, textrect, Qt::AlignCenter,
  44. option.palette, parent->isEnabled(), text);
  45.  
  46. } else {
  47. QStyledItemDelegate::paint(painter, option, index);
  48. }
  49. }
  50.  
  51. QSize SheetDelegate::sizeHint(const QStyleOptionViewItem &opt, const QModelIndex &index) const
  52. {
  53. QStyleOptionViewItem option = opt;
  54. QSize sz = QStyledItemDelegate::sizeHint(opt, index) + QSize(2, 2);
  55. return sz;
  56. }
To copy to clipboard, switch view to plain text mode 

The StencilView class is defined as a QWidget class where shows the result.

StencilView.h
Qt Code:
  1. #ifndef STENCILVIEW_H
  2. #define STENCILVIEW_H
  3.  
  4. #include "SheetDelegate.h"
  5.  
  6. #include <QWidget>
  7.  
  8. class StencilSelector;
  9. class QFileSystemModel;
  10. class QTreeView;
  11. class QLineEdit;
  12. class SheetDelegate;
  13. class StencilView : public QWidget
  14. {
  15. Q_OBJECT
  16.  
  17. public:
  18. StencilView(StencilSelector *parent);
  19.  
  20. private slots:
  21. void reapplyFilter();
  22.  
  23. private:
  24. QTreeView *treeView;
  25. SheetDelegate *delegate;
  26. QFileSystemModel *sourceModel;
  27. QSortFilterProxyModel *proxyModel;
  28. QLineEdit *filterLineEdit;
  29. QPushButton *moreButton;
  30. };
  31.  
  32. #endif
To copy to clipboard, switch view to plain text mode 

StencilView.cpp
Qt Code:
  1. #include <QtGui>
  2.  
  3. #include "StencilView.h"
  4. #include "StencilSelector.h"
  5.  
  6. StencilView::StencilView(StencilSelector *parent): QWidget(parent)
  7. {
  8. sourceModel = new QFileSystemModel;
  9. sourceModel->setReadOnly(true);
  10. sourceModel->setFilter(QDir::AllEntries);
  11. //sourceModel->setNameFilters(QStringList::QStringList("*.odg"));
  12. sourceModel->setNameFilterDisables(false);
  13. sourceModel->setRootPath("/home/yue/stencils_test");
  14. //QModelIndex index = sourceModel->rootPath();
  15.  
  16. proxyModel = new QSortFilterProxyModel;
  17. proxyModel->setSourceModel(sourceModel);
  18. proxyModel->setFilterKeyColumn(-1);
  19. proxyModel->setDynamicSortFilter(true);
  20.  
  21. delegate = new SheetDelegate(this);
  22. treeView = new QTreeView;
  23. treeView->setModel(proxyModel);
  24. treeView->setItemDelegate(delegate);
  25. /*treeView->header()->setStretchLastSection(true);
  26.   treeView->header()->setSortIndicator(0, Qt::AscendingOrder);
  27.   treeView->header()->setSortIndicatorShown(true);
  28.   treeView->header()->setClickable(true);
  29.   */
  30.  
  31. //treeView->setRootIndex(proxyModel->mapFromSource(index));
  32.  
  33. filterLineEdit = new QLineEdit;
  34.  
  35. connect(filterLineEdit, SIGNAL(textChanged(const QString &)),
  36. this, SLOT(reapplyFilter()));
  37.  
  38. moreButton = new QPushButton("...");
  39. QVBoxLayout *mainLayout = new QVBoxLayout;
  40. QHBoxLayout *interLayout = new QHBoxLayout;
  41.  
  42. interLayout->addWidget(moreButton);
  43. interLayout->addWidget(filterLineEdit);
  44. mainLayout->addLayout(interLayout);
  45. mainLayout->addWidget(treeView);
  46. setLayout(mainLayout);
  47. //filterLineEdit->setText(tr("<Filter>"));
  48. //setWindowTitle(tr("Stencil View"));
  49. }
  50.  
  51. void StencilView::reapplyFilter()
  52. {
  53. QRegExp regExp(filterLineEdit->text(), Qt::CaseInsensitive, QRegExp::RegExp);
  54. proxyModel->setFilterRegExp(regExp);
  55. }
  56.  
  57. #include <StencilView.moc>
To copy to clipboard, switch view to plain text mode 

Could anyone point out what's wrong there?