PDA

View Full Version : segfaulting delegate, please help



janorcutt
23rd February 2010, 22:26
hi, im having a big problem with my custom delegate, heres the code from the mainwindow class:


mediaItemDelegate *delegate;
ui->listView_2->setModel(model);
ui->listView_2->setItemDelegate(delegate);

and the delegate itself


class mediaItemDelegate : public QItemDelegate
{
Q_OBJECT
public:
mediaItemDelegate(QObject *parent = 0);

void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const = 0;
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const = 0;

};


#include "mediaitemdelegate.h"

mediaItemDelegate::mediaItemDelegate(QObject *parent):
QItemDelegate(parent)
{
}

void mediaItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if(index.isValid()){
const int left = option.rect.left();
const int top = option.rect.top();
const int width = option.rect.width();
QFont titlefont(option.font);
titlefont.setBold(true);
titlefont.setPointSize(titlefont.pointSize());
int height = (2 * titlefont.pixelSize()) + 10;
int padding = 3;
QColor foregroundColor = (option.state.testFlag(QStyle::State_Selected))?
option.palette.color(QPalette::HighlightedText):op tion.palette.color(QPalette::Text);
//base pixmap
QPixmap pixmap(width, height);
pixmap.fill(Qt::transparent);
QPainter p(&pixmap);
p.translate(-option.rect.topLeft());

//icon time
QIcon icon;
int iconwidth = height - 6;
icon.addFile(QString::fromUtf8(":/icons/icons/hi48-action-filename-filetype-amarok.png"), QSize(iconwidth,iconwidth),QIcon::Normal, QIcon::On);
if(!icon.isNull()){
icon.paint(&p, left + padding, top + padding, iconwidth, iconwidth, Qt::AlignCenter, QIcon::Normal);
}

//now for text
QString artist, album, title,duration;
QMap<int, QVariant> map(index.model()->itemData(index));
title.append(map.value(mediaItem::TitleRole).toStr ing());
artist.append(map.value(mediaItem::ArtistRole).toS tring());
album.append(map.value(mediaItem::AlbumRole).toStr ing());
duration.append(map.value(mediaItem::DurationRole) .toString());

QFont font(option.font);
font.setBold(true);
int lefttext = iconwidth + 2 * padding;
int topoff = (height - iconwidth) / 2;
QTextOption txtopt(Qt::AlignLeft | Qt::AlignBottom);
txtopt.setWrapMode(QTextOption::WordWrap);
QRect titlerect(left + lefttext, top + padding, (width / 2) - padding - lefttext, topoff);
QRect artistrect(left + lefttext, top + (2*padding) + topoff + 1, (width / 2) - padding - lefttext, topoff);
QRect albumrect(left + lefttext + titlerect.width() + padding, top + (2*padding) + topoff + 1, (width / 2) - padding - lefttext, topoff);
QRect durrect(left + lefttext + titlerect.width() + padding, top + padding, (width / 2) - padding - lefttext, topoff);
font.setPixelSize(titlerect.height()/7);
p.setFont(font);
p.setPen(foregroundColor);
p.drawText(QRectF(titlerect), title, txtopt);
p.drawText(QRectF(artistrect), artist, txtopt);
txtopt.setAlignment(Qt::AlignRight | Qt::AlignBottom);
p.drawText(QRectF(albumrect), album, txtopt);
p.drawText(QRectF(durrect), duration, txtopt);
p.end();

//hey presto
painter->drawPixmap(option.rect.topLeft(), pixmap);
/*
QPixmap reflect = Utilities::reflection(pixmap);
painter->drawPixmap(option.rect.topleft() + QPoint(0, size.width() + 1), reflect);
*/
}
}

QSize mediaItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if(!index.isValid()){
return QSize(0, 0);
}

int width = 0;
QFont titlefont(option.font);
titlefont.setBold(true);
titlefont.setPointSize(titlefont.pointSize());
int height = (2 * titlefont.pixelSize()) + 10;
return QSize(width, height);
}

if anybody can shed some light on why this is segfaulting when i try to the the delegate with QListView::setItemDelegate I would be grateful as im nearly out of bourbon lol

ta janorcutt :confused:

Ginsengelf
24th February 2010, 07:57
Hi, in your mainwindow code, you just create a pointer to a delegate, but no object.
Use
mediaItemDelegate *delegate = new mediaItemDelegate (/*insert parent here*/);

Ginsengelf

janorcutt
24th February 2010, 08:21
thanks i did that, and now it doesn't compile. I get


/home/james/myTunes/mytunes.cpp:10: error: cannot allocate an object of abstract type ‘mediaItemDelegate’
/home/james/myTunes/mediaitemdelegate.h:25: note: because the following virtual functions are pure within ‘mediaItemDelegate’:
/home/james/myTunes/mediaitemdelegate.h:30: note: virtual void mediaItemDelegate::paint(QPainter*, const QStyleOptionViewItem&, const QModelIndex&) const
/home/james/myTunes/mediaitemdelegate.h:31: note: virtual QSize mediaItemDelegate::sizeHint(const QStyleOptionViewItem&, const QModelIndex&) const

i've declared the delegate class in the header, and included the delegate's source, and i've created the object.
is it something immensely simple that i'm missing here??

Lykurg
24th February 2010, 09:20
is it something immensely simple that i'm missing here??
If you ask so: Yes ;) You don't have to declare virtual functions you have to reimp them! So just delete the two " = 0;" from your header file and all should work fine.

janorcutt
24th February 2010, 09:55
thanks i just figured that out for myself, that'll teach me to cut and paste :rolleyes:

thanks for the help anyway

janorcutt :o