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>
{
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)));
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);
}
}
{
}
#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);
}
To copy to clipboard, switch view to plain text mode
PluginDelegate.h
{
Q_OBJECT
public:
};
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;
};
To copy to clipboard, switch view to plain text mode
showhistory.cpp
showhistory
::showhistory(QWidget *parent
){
ui.setupUi(this);
window->setWindowTitle("History");
lv.setAutoFillBackground(true);
model.setRowCount(8);
model.setColumnCount(1);
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++;
painter.setPen(Qt::blue);
painter.drawLine(0,linenum*(140) ,230,linenum*(140) );
}
file.close();
}
lv.setModel(&model);
[COLOR="red"]lv.setItemDelegate(new PluginDelegate(&lv));[/COLOR]
lv.setAlternatingRowColors(true);
layout->addWidget(&lv);
window->setLayout(layout);
window->showMaximized();
};
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);
[COLOR="red"]lv.setItemDelegate(new PluginDelegate(&lv));[/COLOR]
lv.setAlternatingRowColors(true);
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(&lv);
window->setLayout(layout);
window->showMaximized();
};
To copy to clipboard, switch view to plain text mode
main.cpp
int main(int argc, char *argv[])
{
showhistory w;
w.show();
return a.exec();
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
showhistory w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
but when i implement it follow code in main.cpp,everything is ok,could anyone tell me why?
public:
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();
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);
}
}
return QSize(200,
52);
// very dumb value }
};
int main(int argc, char *argv[])
{
window->setWindowTitle("History");
lv.setAutoFillBackground(true);
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++;
painter.setPen(Qt::blue);
painter.drawLine(0,linenum*(140) ,230,linenum*(140) );
}
file.close();
}
lv.setModel(&model);
[COLOR="red"] lv.setItemDelegate(new PluginDelegate(&lv));[/COLOR]
lv.setAlternatingRowColors(true);
layout->addWidget(&lv);
window->setLayout(layout);
window->showMaximized();
return a.exec();
}
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);
[COLOR="red"] lv.setItemDelegate(new PluginDelegate(&lv));[/COLOR]
lv.setAlternatingRowColors(true);
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(&lv);
window->setLayout(layout);
window->showMaximized();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
the code is same,but different result?I just want to seperate it from main.cpp
Bookmarks