PDA

View Full Version : Error in custom delegate



philacorns
19th April 2010, 16:05
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


#ifndef SHEET_DELEGATE_H
#define SHEET_DELEGATE_H

#include <QStyledItemDelegate>

class SheetDelegate: public QStyledItemDelegate
{
Q_OBJECT
public:
SheetDelegate(QObject *parent = 0);

virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
virtual QSize sizeHint(const QStyleOptionViewItem &opt, const QModelIndex &index) const;
};

#endif // SHEET_DELEGATE_H


Modified SheetDelegate.cpp


#include <QtGui/QStyle>
#include <QtGui/QPainter>


SheetDelegate::SheetDelegate(QObject *parent)
: QStyledItemDelegate(parent)
{
}

void SheetDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if (index.model()->parent(index).isValid()) {
// this is a top-level item.
QStyleOptionButton buttonOption;

buttonOption.state = option.state;
#ifdef Q_WS_MAC
buttonOption.state |= QStyle::State_Raised;
#endif
buttonOption.state &= ~QStyle::State_HasFocus;

buttonOption.rect = option.rect;
buttonOption.palette = option.palette;
buttonOption.features = QStyleOptionButton::None;
QStyle::drawControl(QStyle::CE_PushButton, &buttonOption, painter, parent);

QStyleOption branchOption;
static const int i = 9; // ### hardcoded in qcommonstyle.cpp
QRect r = option.rect;
branchOption.rect = QRect(r.left() + i/2, r.top() + (r.height() - i)/2, i, i);
branchOption.palette = option.palette;
branchOption.state = QStyle::State_Children;

if (parent->isExpanded(index))
branchOption.state |= QStyle::State_Open;

QStyle::drawPrimitive(QStyle::PE_IndicatorBranch, &branchOption, painter, parent);

// draw text
QRect textrect = QRect(r.left() + i*2, r.top(), r.width() - ((5*i)/2), r.height());
QString text = elidedText(option.fontMetrics, textrect.width(), Qt::ElideMiddle,
index.model()->data(index, Qt::DisplayRole).toString());
QStyle::drawItemText(painter, textrect, Qt::AlignCenter,
option.palette, parent->isEnabled(), text);

} else {
QStyledItemDelegate::paint(painter, option, index);
}
}

QSize SheetDelegate::sizeHint(const QStyleOptionViewItem &opt, const QModelIndex &index) const
{
QStyleOptionViewItem option = opt;
QSize sz = QStyledItemDelegate::sizeHint(opt, index) + QSize(2, 2);
return sz;
}


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

StencilView.h


#ifndef STENCILVIEW_H
#define STENCILVIEW_H

#include "SheetDelegate.h"

#include <QWidget>

class StencilSelector;
class QFileSystemModel;
class QSortFilterProxyModel;
class QTreeView;
class QLineEdit;
class QPushButton;
class SheetDelegate;
class StencilView : public QWidget
{
Q_OBJECT

public:
StencilView(StencilSelector *parent);

private slots:
void reapplyFilter();

private:
QTreeView *treeView;
SheetDelegate *delegate;
QFileSystemModel *sourceModel;
QSortFilterProxyModel *proxyModel;
QLineEdit *filterLineEdit;
QPushButton *moreButton;
};

#endif


StencilView.cpp


#include <QtGui>

#include "StencilView.h"
#include "StencilSelector.h"

StencilView::StencilView(StencilSelector *parent): QWidget(parent)
{
sourceModel = new QFileSystemModel;
sourceModel->setReadOnly(true);
sourceModel->setFilter(QDir::AllEntries);
//sourceModel->setNameFilters(QStringList::QStringList("*.odg"));
sourceModel->setNameFilterDisables(false);
sourceModel->setRootPath("/home/yue/stencils_test");
//QModelIndex index = sourceModel->rootPath();

proxyModel = new QSortFilterProxyModel;
proxyModel->setSourceModel(sourceModel);
proxyModel->setFilterKeyColumn(-1);
proxyModel->setDynamicSortFilter(true);

delegate = new SheetDelegate(this);
treeView = new QTreeView;
treeView->setModel(proxyModel);
treeView->setItemDelegate(delegate);
/*treeView->header()->setStretchLastSection(true);
treeView->header()->setSortIndicator(0, Qt::AscendingOrder);
treeView->header()->setSortIndicatorShown(true);
treeView->header()->setClickable(true);
*/

//treeView->setRootIndex(proxyModel->mapFromSource(index));

filterLineEdit = new QLineEdit;

connect(filterLineEdit, SIGNAL(textChanged(const QString &)),
this, SLOT(reapplyFilter()));

moreButton = new QPushButton("...");
QVBoxLayout *mainLayout = new QVBoxLayout;
QHBoxLayout *interLayout = new QHBoxLayout;

interLayout->addWidget(moreButton);
interLayout->addWidget(filterLineEdit);
mainLayout->addLayout(interLayout);
mainLayout->addWidget(treeView);
setLayout(mainLayout);
//filterLineEdit->setText(tr("<Filter>"));
//setWindowTitle(tr("Stencil View"));
}

void StencilView::reapplyFilter()
{
QRegExp regExp(filterLineEdit->text(), Qt::CaseInsensitive, QRegExp::RegExp);
proxyModel->setFilterRegExp(regExp);
}

#include <StencilView.moc>


Could anyone point out what's wrong there?

Lykurg
19th April 2010, 16:17
You have to put
#inlcude "SheetDelegate.h" in your SheetDelegate.cpp file.

philacorns
20th April 2010, 00:43
I've included that line. Just missed it when copying code.

Lykurg
20th April 2010, 07:08
And in your StencilView.cpp?

philacorns
21st April 2010, 05:41
Included in StencilView.h