There is a model, containing strings, and a list view.
The QListView
is a descendant of QPaintDevice
, so you can override paintEvent (QPaintEvent *)
in it and draw something on it. For example, you need to draw image cheker.png:
#include <QApplication>
#include <QPainter>
#include <QPaintEvent>
#include <QListView>
#include <QStringListModel>
class View :public QListView {
public:
void paintEvent(QPaintEvent* event) {
QListView::paintEvent(event);
QPainter painter(this);
painter.drawPixmap(10,10,QPixmap("cheker.png"));
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QStringListModel model;
QStringList list;
list << "a" << "b" << "c" << "d" << "e" << "f" << "g" << "h";
model.setStringList(list);
View view;
view.setModel(&model);
view.show();
return a.exec();
}
What is expected is not happening - the picture does not show. The view is depicted as if no methods were overridden. And the following is printed in the console:
Actual results:
What I expect:
So, how to draw in a view?