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);
};
#endif // SHEET_DELEGATE_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
To copy to clipboard, switch view to plain text mode
Modified SheetDelegate.cpp
#include <QtGui/QStyle>
#include <QtGui/QPainter>
SheetDelegate
::SheetDelegate(QObject *parent
) : QStyledItemDelegate(parent)
{
}
{
if (index.model()->parent(index).isValid()) {
// this is a top-level item.
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;
QStyle::drawControl(QStyle::CE_PushButton,
&buttonOption, painter, parent
);
static const int i = 9; // ### hardcoded in qcommonstyle.cpp
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 sz
= QStyledItemDelegate
::sizeHint(opt, index
) + QSize(2,
2);
return sz;
}
#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;
}
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
#ifndef STENCILVIEW_H
#define STENCILVIEW_H
#include "SheetDelegate.h"
#include <QWidget>
class StencilSelector;
class QFileSystemModel;
class SheetDelegate;
{
Q_OBJECT
public:
StencilView(StencilSelector *parent);
private slots:
void reapplyFilter();
private:
SheetDelegate *delegate;
QFileSystemModel *sourceModel;
};
#endif
#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
To copy to clipboard, switch view to plain text mode
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->setSourceModel(sourceModel);
proxyModel->setFilterKeyColumn(-1);
proxyModel->setDynamicSortFilter(true);
delegate = new SheetDelegate(this);
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));
connect(filterLineEdit,
SIGNAL(textChanged
(const QString &)),
this, SLOT(reapplyFilter()));
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>
#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>
To copy to clipboard, switch view to plain text mode
Could anyone point out what's wrong there?
Bookmarks