Goal: Creating an item delegate with custom textual content to be used in QListView.
Problem: Drawing text with QPainter in a reimplementation of the paint()
method of a QAbstractItemDelegate's subclass is noticably slower than drawing shapes and pixmaps. Changing the base class to QStyledItemDelegate does not improve the speed.
Setup: Qt 5.9.1, MSVC 2017, tested on Windows 7/10
Pre-research: A similar bug is reported here, however in this particular case the performance issue exists even if QPainter::setFont()
is not used.
The Qt examples on item delegates do not help much, as they show how to draw controls, not just text.
Example: The example given below illustrates the problem. After showing the application's window, the content of the QListView is shown with a slight, but noticable delay. In some cases this delay lasts up to several seconds. When either of view->setItemDelegate(new Delegate(this));
or painter->drawText(0, option.rect.y() + 18, index.data().toString());
are commented there is no observable delay between showing the window and the content of the QListView.
MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QListView>
#include <QStandardItemModel>
#include "Delegate.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "MainWindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
QStandardItemModel *model = new QStandardItemModel(this);
QListView *view = new QListView(this);
view->setModel(model);
view->setItemDelegate(new Delegate(this));
for (int n = 0; n < 100; n++) { model->appendRow(new QStandardItem("Test " + QString::number(n))); }
setCentralWidget(view);
}
Delegate.h
#ifndef DELEGATE_H
#define DELEGATE_H
#include <QAbstractItemDelegate>
#include <QPainter>
class Delegate : public QAbstractItemDelegate
{
Q_OBJECT
public:
explicit Delegate(QObject *parent = nullptr);
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
};
#endif // DELEGATE_H
Delegate.cpp
#include "Delegate.h"
Delegate::Delegate(QObject *parent) : QAbstractItemDelegate(parent)
{
}
void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
painter->drawText(0, option.rect.y() + 18, index.data().toString());
painter->drawLine(0, option.rect.y() + 19, option.rect.width(), option.rect.y() + 19);
}
QSize Delegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
return QSize(0, 20);
}