Results 1 to 5 of 5

Thread: Error in custom delegate

  1. #1
    Join Date
    Apr 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question Error in custom delegate

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Error in custom delegate

    You have to put
    Qt Code:
    1. #inlcude "SheetDelegate.h"
    To copy to clipboard, switch view to plain text mode 
    in your SheetDelegate.cpp file.

  3. #3
    Join Date
    Apr 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Error in custom delegate

    I've included that line. Just missed it when copying code.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Error in custom delegate

    And in your StencilView.cpp?

  5. #5
    Join Date
    Apr 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Error in custom delegate

    Included in StencilView.h

Similar Threads

  1. Custom Model? Custom View? Custom Delegate?
    By Doug Broadwell in forum Newbie
    Replies: 4
    Last Post: 11th February 2010, 21:23
  2. Replies: 0
    Last Post: 1st February 2010, 12:00
  3. Question about custom view (or custom delegate)
    By skuda in forum Qt Programming
    Replies: 1
    Last Post: 21st September 2009, 21:06
  4. Save problem with custom delegate
    By Banjo in forum Qt Programming
    Replies: 0
    Last Post: 22nd July 2008, 03:34
  5. Capture events in a custom delegate
    By vfernandez in forum Qt Programming
    Replies: 9
    Last Post: 19th March 2008, 06:33

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.