PDA

View Full Version : about QAbstractItemDelegate



drizzlefsh
10th September 2009, 11:09
I implement QAbstractItemDelegate's Paint and sizeHint,But data can't display.follow is code ,does anybody help me?
PluginDelegate.cpp

#include"PluginDelegate.h"
#include <QPainter>

void PluginDelegate::paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
if(option.state & QStyle::State_Selected)
{
painter->fillRect(option.rect, option.palette.color(QPalette::Highlight));
}
QIcon ic = QIcon(qvariant_cast<QPixmap>(index.data(Qt::DecorationRole)));
QString txt;
QString txt1 = "shit";
for(int i=0;i<strlist.count();i++)
{
txt = strlist.at(i++).toAscii();
txt1 = strlist.at(i++).toAscii();
}

QRect r = option.rect.adjusted(2, 2, -2, -2);
ic.paint(painter, r, Qt::AlignVCenter|Qt::AlignLeft);
r = r.adjusted(r.height()+20, 0, 0, 0);
if(txt!=""&txt1!="")
{
painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignBottom|Qt::AlignLeft|Qt::TextWordWrap, txt, &r);
r = r.adjusted(0, 0, 0, 0);
painter->drawText(r.left()+60, r.top(), r.width()+20, r.height(), Qt::AlignTop|Qt::AlignLeft|Qt::TextWordWrap, txt1, &r);
}
}
QSize PluginDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
return QSize(200, 52);
}

PluginDelegate.h

class PluginDelegate : public QAbstractItemDelegate
{
Q_OBJECT

public:
PluginDelegate(QObject *parent=0) : QAbstractItemDelegate(parent){};

virtual void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const;

virtual QSize sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const;

};

showhistory.cpp

showhistory::showhistory(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
QWidget* window = new QWidget;
window->setWindowTitle("History");

QListView lv;
lv.setAutoFillBackground(true);
QStandardItemModel model;
model.setRowCount(8);
model.setColumnCount(1);

QTextStream floStream(&file);
QTextCodec *codec=QTextCodec::codecForName("GBK");
floStream.setCodec(codec);
int linenum=1;
line = codec->fromUnicode(floStream.readLine());
model.setData(model.index(count, 0), QPixmap(":Image/butterfly.png"), Qt::DecorationRole);
model.setData(model.index(count++, 0), line);
linenum++;
QPainter painter(&lv);
painter.setPen(Qt::blue);
painter.drawLine(0,linenum*(140) ,230,linenum*(140) );
}
file.close();
}

lv.setModel(&model);
lv.setItemDelegate(new PluginDelegate(&lv));
lv.setAlternatingRowColors(true);

QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(&lv);
window->setLayout(layout);
window->showMaximized();
};

main.cpp

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
showhistory w;
w.show();
return a.exec();
}

but when i implement it follow code in main.cpp,everything is ok,could anyone tell me why?

class PluginDelegate : public QAbstractItemDelegate {
public:
PluginDelegate(QObject *parent=0) : QAbstractItemDelegate(parent){}
void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const{
if(option.state & QStyle::State_Selected){
painter->fillRect(option.rect, option.palette.color(QPalette::Highlight));
}
QIcon ic = QIcon(qvariant_cast<QPixmap>(index.data(Qt::DecorationRole)));
QString str = index.data(Qt::DisplayRole).toString();
QStringList strlist = str.split(";");
QString txt;
QString txt1 = "shit";
for(int i=0;i<strlist.count();i++)
{
txt = strlist.at(i++).toAscii();
txt1 = strlist.at(i++).toAscii();
}

QRect r = option.rect.adjusted(2, 2, -2, -2);
ic.paint(painter, r, Qt::AlignVCenter|Qt::AlignLeft);
r = r.adjusted(r.height()+20, 0, 0, 0);
if(txt!=""&txt1!="")
{
painter->drawText(r.left(), r.top(), r.width(), r.height(), Qt::AlignBottom|Qt::AlignLeft|Qt::TextWordWrap, txt, &r);
r = r.adjusted(0, 0, 0, 0);
painter->drawText(r.left()+60, r.top(), r.width()+20, r.height(), Qt::AlignTop|Qt::AlignLeft|Qt::TextWordWrap, txt1, &r);
}
}
QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const{
return QSize(200, 52); // very dumb value
}

};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget* window = new QWidget;
window->setWindowTitle("History");

QListView lv;
lv.setAutoFillBackground(true);
QStandardItemModel model;
model.setRowCount(8);
model.setColumnCount(1);

model.setData(model.index(count, 0), QPixmap(":/butterfly.png"), Qt::DecorationRole);
model.setData(model.index(count++, 0), line);
linenum++;
QPainter painter(&lv);
painter.setPen(Qt::blue);
painter.drawLine(0,linenum*(140) ,230,linenum*(140) );
}
file.close();
}

lv.setModel(&model);
lv.setItemDelegate(new PluginDelegate(&lv));
lv.setAlternatingRowColors(true);

QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(&lv);
window->setLayout(layout);
window->showMaximized();
return a.exec();
}
the code is same,but different result?I just want to seperate it from main.cpp

drizzlefsh
10th September 2009, 11:11
And Paint never be called when I set up a new cpp file for PluginDelegate,why,how i can call paint function

victor.fernandez
10th September 2009, 16:23
Do not create the model, the view and the delegate on the stack! Create them in the heap. Otherwise they will be destroyed after the constructor finishes its execution.


QListView *lv = new QListView(this);
lv->setAutoFillBackground(true);
QStandardItemModel *model = new QStandardItemModel(this);
model->setRowCount(8);
model->setColumnCount(1);
...
lv->setModel(model);
lv->setItemDelegate(new PluginDelegate(lv));

drizzlefsh
11th September 2009, 03:57
HI,maybe you are right,but problem still exist.problem occur not because of this stack.in my opinion.

drizzlefsh
11th September 2009, 08:01
thanks for your help,problem has been resolve.i define them in .h file.things is expected.