Results 1 to 3 of 3

Thread: cannot instantiate abstract class

  1. #1
    Join Date
    May 2013
    Location
    Georgia,Tbilisi
    Posts
    32
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default cannot instantiate abstract class

    Hello all.
    I'm reading Johan Thelin's "Foundation of Qt Development" And I'm at Costum View. It is little bit difficult and i write code such as in the book.
    I Dropped TableView on a form and then:
    MainWindow Constructor:
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6.  
    7.  
    8. for(int i = 0; i < 10; i++){
    9. QStandardItem *item = new QStandardItem(QString("Row: %1").arg(i));
    10. item->setEditable(false);
    11. model->setItem(i,0,item);
    12.  
    13. model->setItem(i,1,new QStandardItem(QString::number((i*30)%100)));
    14. }
    15.  
    16. ui->tableView->setModel(model);
    17.  
    18. BarDelegate dg;
    19. ui->tableView->setItemDelegateForColumn(1,&dg);
    20. }
    To copy to clipboard, switch view to plain text mode 

    But I get such error:
    Capture.jpg

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

    Qt Code:
    1. #include "bardelegate.h"
    2.  
    3. BarDelegate::BarDelegate(QObject *parent)
    4. {
    5.  
    6. }
    7. QSize BarDelegate::sizeHint(const QStyleOptionViewItem *option, const QModelIndex &index) const
    8. {
    9. return QSize(45,15);
    10. }
    11.  
    12. void BarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex *index) const
    13. {
    14. if(option.state & QStyle::State_Selected)
    15. painter->fillRect(option.rect,option.palette.highlight() );
    16.  
    17. int value = index->model()->data(index,Qt::DisplayRole).toInt();
    18. double factor = (double)value/100.0;
    19.  
    20. painter->save();
    21.  
    22. if(factor > 1)
    23. {
    24. painter->setBrush(Qt::red);
    25. factor = 1;
    26. }
    27. else
    28. painter->setBrush(QColor(0,(int)(factor*255),255-(int)(factor*255)));
    29.  
    30. painter->setPen(Qt::black);
    31. painter->drawRect(option.rect.x()+2,option.rect.y()+2,(int)(factor*(option.rect.width()-5)),option.rect.height()-5);
    32.  
    33. painter->restore();
    34. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2013
    Location
    Georgia,Tbilisi
    Posts
    32
    Thanks
    7
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Lightbulb Re: cannot instantiate abstract class

    Solved!!!
    I just didn't correctly override paint and sizeHint methods

    As it seems pure virtual functions(paint and sizeHint) has been changed after that book released.
    Differences is in parameters.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: cannot instantiate abstract class

    The delegate class paint() function has never taken a pointer to QModelIndex as its third argument, and the sizeHint() has never taken a pointer as its first argument. These look like typos in the book.

Similar Threads

  1. How to instantiate a class if I dont know its name?
    By anoraxis in forum Qt Programming
    Replies: 5
    Last Post: 20th March 2012, 22:42
  2. Abstract base class and inherited class members
    By JovianGhost in forum General Programming
    Replies: 3
    Last Post: 19th March 2010, 13:32
  3. Instantiate objects only having their class name
    By victor.fernandez in forum Qt Programming
    Replies: 1
    Last Post: 30th June 2009, 17:22
  4. wanted to re-instantiate two classes from a class
    By salmanmanekia in forum General Programming
    Replies: 2
    Last Post: 22nd August 2008, 09:59
  5. Replies: 2
    Last Post: 25th August 2006, 12:35

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.