在Qt C ++中在自定义项目委托上绘画文本时的性能问题 [英] Performance issues when painting text on custom item delegates in Qt C++

查看:78
本文介绍了在Qt C ++中在自定义项目委托上绘画文本时的性能问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标:创建具有自定义文本内容的项目委托,以在 QListView .

Goal: Creating an item delegate with custom textual content to be used in QListView.

问题:使用 QPainter 绘制文本重新实现 QAbstractItemDelegate 的子类明显比绘制形状和像素图慢.将基类更改为 QStyledItemDelegate 不会提高速度.

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.

设置:Qt 5.9.1,MSVC 2017,已在Windows 7/10上进行了测试

Setup: Qt 5.9.1, MSVC 2017, tested on Windows 7/10

预研究:此处a>,但是在这种特殊情况下,即使不使用 QPainter :: setFont(),也会存在性能问题.关于项目委托的Qt示例并没有多大帮助,因为它们显示了如何绘制控件,而不仅仅是文本.

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.

示例:以下示例说明了该问题.显示应用程序窗口后,显示 QListView 的内容时会出现轻微但明显的延迟.在某些情况下,这种延迟会持续长达几秒钟.当 view-> setItemDelegate(new Delegate(this)); painter-> drawText(0,option.rect.y()+ 18,index.data().toString()); 被注释,显示窗口和 QListView 的内容之间没有明显的延迟.

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);
}

推荐答案

目前,我已经创建了

For now I've created a bug report. I will update this post with any further info I receive.

这篇关于在Qt C ++中在自定义项目委托上绘画文本时的性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆