1 Attachment(s)
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:
Code:
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
for(int i = 0; i < 10; i++){
item->setEditable(false);
model->setItem(i,0,item);
}
ui->tableView->setModel(model);
BarDelegate dg;
ui->tableView->setItemDelegateForColumn(1,&dg);
}
But I get such error:
Attachment 9910
Here is BarDelegate declaration and definition:
Code:
#include "bardelegate.h"
BarDelegate
::BarDelegate(QObject *parent
){
}
{
}
{
if(option.
state & QStyle::State_Selected) painter->fillRect(option.rect,option.palette.highlight() );
int value = index->model()->data(index,Qt::DisplayRole).toInt();
double factor = (double)value/100.0;
painter->save();
if(factor > 1)
{
painter->setBrush(Qt::red);
factor = 1;
}
else
painter
->setBrush
(QColor(0,
(int)(factor
*255),
255-(int)(factor
*255)));
painter->setPen(Qt::black);
painter->drawRect(option.rect.x()+2,option.rect.y()+2,(int)(factor*(option.rect.width()-5)),option.rect.height()-5);
painter->restore();
}
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.
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.