带有自定义项目和自定义项目小部件的 QListView/QListWidget [英] QListView/QListWidget with custom items and custom item widgets

查看:41
本文介绍了带有自定义项目和自定义项目小部件的 QListView/QListWidget的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写 PyQt 应用程序,但在创建自定义列表视图时遇到了一些问题.我希望列表包含任意小部件(特别是一个自定义小部件).我该怎么办?

I'm writing a PyQt application and am having some trouble creating a custom list view. I'd like the list to contain arbitrary widgets (one custom widget in particular). How would I go about this?

似乎替代方法是创建一个包裹在滚动条中的表格或网格视图.但是,我希望能够利用模型/视图方法以及嵌套(树视图)支持内置句柄.

It seems that the alternative would be to create a table or grid view wrapped in a scrollbar. However, I'd like to be able to take advantage of the model/view approach as well as the nesting (tree-view) support the built-ins handle.

澄清一下,自定义小部件是交互式的(包含按钮),因此解决方案需要的不仅仅是绘制小部件.

To clarify, the custom widgets are interactive (contain buttons), so the solution requires more than painting a widget.

推荐答案

我认为你需要子类化 QItemDelegate.

I think you need to subclass QItemDelegate.

QItemDelegate 可以用来提供自定义显示功能和编辑器基于项目视图的小部件QAbstractItemView 子类.用一个为此目的的委托允许显示和编辑机制自主定制开发从模型和视图.

QItemDelegate can be used to provide custom display features and editor widgets for item views based on QAbstractItemView subclasses. Using a delegate for this purpose allows the display and editing mechanisms to be customized and developed independently from the model and view.

此代码取自 Qt 的示例,即 torrent 应用程序.

This code is taken from Qt's examples, the torrent application.

class TorrentViewDelegate : public QItemDelegate
{
    Q_OBJECT
public:
    inline TorrentViewDelegate(MainWindow *mainWindow) : QItemDelegate(mainWindow) {}

    inline void paint(QPainter *painter, const QStyleOptionViewItem &option,
                      const QModelIndex &index ) const
    {
        if (index.column() != 2) {
            QItemDelegate::paint(painter, option, index);
            return;
        }

        // Set up a QStyleOptionProgressBar to precisely mimic the
        // environment of a progress bar.
        QStyleOptionProgressBar progressBarOption;
        progressBarOption.state = QStyle::State_Enabled;
        progressBarOption.direction = QApplication::layoutDirection();
        progressBarOption.rect = option.rect;
        progressBarOption.fontMetrics = QApplication::fontMetrics();
        progressBarOption.minimum = 0;
        progressBarOption.maximum = 100;
        progressBarOption.textAlignment = Qt::AlignCenter;
        progressBarOption.textVisible = true;

        // Set the progress and text values of the style option.
        int progress = qobject_cast<MainWindow *>(parent())->clientForRow(index.row())->progress();
        progressBarOption.progress = progress < 0 ? 0 : progress;
        progressBarOption.text = QString().sprintf("%d%%", progressBarOption.progress);

        // Draw the progress bar onto the view.
        QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter);
    }
};

基本上如您所见,它会检查要绘制的列是否具有特定索引,如果是,则绘制进度条.我认为你可以稍微调整一下,而不是使用 QStyleOption 你可以使用你自己的小部件.

Basically as you can see it checks if the column to be painted is of a specific index, if so it paints a progress bar. I think you could tweak it a little and instead of using a QStyleOption you could use your own widget.

不要忘记使用 setItemDelegate.

在调查您的问题时,我偶然发现了 this 线程,详细说明了如何使用 QItemDelegate 绘制自定义小部件,我相信它包含您可能需要的所有信息.

While investigating your question I've stumbled upon this thread, which elaborates how to paint a custom widget using a QItemDelegate, I believe it has all the info you might need.

这篇关于带有自定义项目和自定义项目小部件的 QListView/QListWidget的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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