- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
目标:创建一个带有自定义文本内容的项目委托(delegate),用于 QListView .
问题:使用 QPainter 绘制文本在一个 QAbstractItemDelegate 的 paint()
方法的重新实现中的子类明显比绘制形状和像素图慢。将基类更改为 QStyledItemDelegate不会提高速度。
设置:Qt 5.9.1,MSVC 2017,在 Windows 7/10 上测试
预研: 报告了类似的bug here ,但是在这种特殊情况下,即使未使用 QPainter::setFont()
也会存在性能问题。项目委托(delegate)的 Qt 示例没有多大帮助,因为它们展示了如何绘制控件,而不仅仅是文本。
示例: 下面给出的示例说明了这个问题。显示应用程序窗口后,QListView 的内容会出现轻微但明显的延迟。在某些情况下,这种延迟会持续到几秒钟。当 view->setItemDelegate(new Delegate(this));
或 painter->drawText(0, option.rect.y() + 18, index.data().toString ());
评论说在显示窗口和 QListView 的内容之间没有明显的延迟。
主窗口.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
主窗口.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);
}
最佳答案
现在我创建了一个 bug report .我会用收到的任何进一步信息更新这篇文章。
关于c++ - 在 Qt C++ 中的自定义项委托(delegate)上绘制文本时的性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45485492/
我正在实现谱聚类算法,我必须确保矩阵(拉普拉斯矩阵)是半正定矩阵。 检查矩阵是否为正定矩阵 (PD) 就足够了,因为可以在特征值中看到“半”部分。矩阵非常大(nxn,其中 n 是几千的数量级)所以特征
我是一名优秀的程序员,十分优秀!