I tried following the MVF video but wasnt able to get what I wrote down to compile:
main.cpp
#include <QtGui/QApplication>
#include "delegate.h"
int main(int argc, char *argv[])
{
//Widget w;
//w.show();
ListDelegate *delegate;
view.setItemDelegate(delegate);
view.show();
return a.exec();
}
#include <QtGui/QApplication>
#include "delegate.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//Widget w;
//w.show();
ListDelegate *delegate;
QListView view;
view.setItemDelegate(delegate);
view.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
delegate.h
#ifndef DELEGATE_H
#define DELEGATE_H
#include <QtGui>
{
public:
};
{
}
void ListDelegate
::paint(QPainter *painter,
{
if(option.
state & QStyle::State_Selected) background = Qt::darkRed;
else
background = Qt::lightGray;
painter->drawText(option.rect, Qt::AlignCenter,
index.model()->data(index).toString());
}
#endif // DELEGATE_H
#ifndef DELEGATE_H
#define DELEGATE_H
#include <QtGui>
class ListDelegate : public QAbstractItemDelegate
{
public:
ListDelegate(QObject *parent=0);
void paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const;
QSize sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index)const;
};
QSize ListDelegate::sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
return QSize(100,40);
}
void ListDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QColor background;
if(option.state & QStyle::State_Selected)
background = Qt::darkRed;
else
background = Qt::lightGray;
painter->setFont(QFont("Arial",12,QFont::Bold));
painter->drawText(option.rect, Qt::AlignCenter,
index.model()->data(index).toString());
}
#endif // DELEGATE_H
To copy to clipboard, switch view to plain text mode
Bookmarks