Results 1 to 5 of 5

Thread: segfaulting delegate, please help

  1. #1
    Join Date
    Feb 2010
    Location
    Wokingham, United Kingdom
    Posts
    36
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question segfaulting delegate, please help

    hi, im having a big problem with my custom delegate, heres the code from the mainwindow class:

    Qt Code:
    1. mediaItemDelegate *delegate;
    2. ui->listView_2->setModel(model);
    3. ui->listView_2->setItemDelegate(delegate);
    To copy to clipboard, switch view to plain text mode 

    and the delegate itself

    Qt Code:
    1. class mediaItemDelegate : public QItemDelegate
    2. {
    3. Q_OBJECT
    4. public:
    5. mediaItemDelegate(QObject *parent = 0);
    6.  
    7. void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const = 0;
    8. QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const = 0;
    9.  
    10. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "mediaitemdelegate.h"
    2.  
    3. mediaItemDelegate::mediaItemDelegate(QObject *parent):
    4. QItemDelegate(parent)
    5. {
    6. }
    7.  
    8. void mediaItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
    9. {
    10. if(index.isValid()){
    11. const int left = option.rect.left();
    12. const int top = option.rect.top();
    13. const int width = option.rect.width();
    14. QFont titlefont(option.font);
    15. titlefont.setBold(true);
    16. titlefont.setPointSize(titlefont.pointSize());
    17. int height = (2 * titlefont.pixelSize()) + 10;
    18. int padding = 3;
    19. QColor foregroundColor = (option.state.testFlag(QStyle::State_Selected))?
    20. option.palette.color(QPalette::HighlightedText):option.palette.color(QPalette::Text);
    21. //base pixmap
    22. QPixmap pixmap(width, height);
    23. pixmap.fill(Qt::transparent);
    24. QPainter p(&pixmap);
    25. p.translate(-option.rect.topLeft());
    26.  
    27. //icon time
    28. QIcon icon;
    29. int iconwidth = height - 6;
    30. icon.addFile(QString::fromUtf8(":/icons/icons/hi48-action-filename-filetype-amarok.png"), QSize(iconwidth,iconwidth),QIcon::Normal, QIcon::On);
    31. if(!icon.isNull()){
    32. icon.paint(&p, left + padding, top + padding, iconwidth, iconwidth, Qt::AlignCenter, QIcon::Normal);
    33. }
    34.  
    35. //now for text
    36. QString artist, album, title,duration;
    37. QMap<int, QVariant> map(index.model()->itemData(index));
    38. title.append(map.value(mediaItem::TitleRole).toString());
    39. artist.append(map.value(mediaItem::ArtistRole).toString());
    40. album.append(map.value(mediaItem::AlbumRole).toString());
    41. duration.append(map.value(mediaItem::DurationRole).toString());
    42.  
    43. QFont font(option.font);
    44. font.setBold(true);
    45. int lefttext = iconwidth + 2 * padding;
    46. int topoff = (height - iconwidth) / 2;
    47. QTextOption txtopt(Qt::AlignLeft | Qt::AlignBottom);
    48. txtopt.setWrapMode(QTextOption::WordWrap);
    49. QRect titlerect(left + lefttext, top + padding, (width / 2) - padding - lefttext, topoff);
    50. QRect artistrect(left + lefttext, top + (2*padding) + topoff + 1, (width / 2) - padding - lefttext, topoff);
    51. QRect albumrect(left + lefttext + titlerect.width() + padding, top + (2*padding) + topoff + 1, (width / 2) - padding - lefttext, topoff);
    52. QRect durrect(left + lefttext + titlerect.width() + padding, top + padding, (width / 2) - padding - lefttext, topoff);
    53. font.setPixelSize(titlerect.height()/7);
    54. p.setFont(font);
    55. p.setPen(foregroundColor);
    56. p.drawText(QRectF(titlerect), title, txtopt);
    57. p.drawText(QRectF(artistrect), artist, txtopt);
    58. txtopt.setAlignment(Qt::AlignRight | Qt::AlignBottom);
    59. p.drawText(QRectF(albumrect), album, txtopt);
    60. p.drawText(QRectF(durrect), duration, txtopt);
    61. p.end();
    62.  
    63. //hey presto
    64. painter->drawPixmap(option.rect.topLeft(), pixmap);
    65. /*
    66.   QPixmap reflect = Utilities::reflection(pixmap);
    67.   painter->drawPixmap(option.rect.topleft() + QPoint(0, size.width() + 1), reflect);
    68.   */
    69. }
    70. }
    71.  
    72. QSize mediaItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
    73. {
    74. if(!index.isValid()){
    75. return QSize(0, 0);
    76. }
    77.  
    78. int width = 0;
    79. QFont titlefont(option.font);
    80. titlefont.setBold(true);
    81. titlefont.setPointSize(titlefont.pointSize());
    82. int height = (2 * titlefont.pixelSize()) + 10;
    83. return QSize(width, height);
    84. }
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    474
    Thanks
    10
    Thanked 62 Times in 60 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: segfaulting delegate, please help

    Hi, in your mainwindow code, you just create a pointer to a delegate, but no object.
    Use
    Qt Code:
    1. mediaItemDelegate *delegate = new mediaItemDelegate (/*insert parent here*/);
    To copy to clipboard, switch view to plain text mode 

    Ginsengelf

  3. #3
    Join Date
    Feb 2010
    Location
    Wokingham, United Kingdom
    Posts
    36
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: segfaulting delegate, please help

    thanks i did that, and now it doesn't compile. I get

    Qt Code:
    1. /home/james/myTunes/mytunes.cpp:10: error: cannot allocate an object of abstract type &#8216;mediaItemDelegate’
    2. /home/james/myTunes/mediaitemdelegate.h:25: note: because the following virtual functions are pure within &#8216;mediaItemDelegate’:
    3. /home/james/myTunes/mediaitemdelegate.h:30: note: virtual void mediaItemDelegate::paint(QPainter*, const QStyleOptionViewItem&, const QModelIndex&) const
    4. /home/james/myTunes/mediaitemdelegate.h:31: note: virtual QSize mediaItemDelegate::sizeHint(const QStyleOptionViewItem&, const QModelIndex&) const
    To copy to clipboard, switch view to plain text mode 

    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??

  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: segfaulting delegate, please help

    Quote Originally Posted by janorcutt View Post
    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.

  5. The following user says thank you to Lykurg for this useful post:

    janorcutt (24th February 2010)

  6. #5
    Join Date
    Feb 2010
    Location
    Wokingham, United Kingdom
    Posts
    36
    Thanks
    7
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Smile Re: segfaulting delegate, please help

    thanks i just figured that out for myself, that'll teach me to cut and paste

    thanks for the help anyway

    janorcutt

Similar Threads

  1. QTableWidget Delegate
    By aekilic in forum Qt Programming
    Replies: 12
    Last Post: 15th May 2009, 09:21
  2. Delegate for a certain item?
    By somebody in forum Qt Programming
    Replies: 1
    Last Post: 18th August 2008, 22:55
  3. Qt 4.2 apps segfaulting
    By IndigoJo in forum Qt Programming
    Replies: 1
    Last Post: 24th June 2008, 23:29
  4. Delegate but when
    By uygar in forum Qt Programming
    Replies: 1
    Last Post: 12th October 2007, 20:28
  5. Why is my program segfaulting?
    By Valheru in forum Qt Programming
    Replies: 11
    Last Post: 1st October 2006, 10:59

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.