在QWidgetDelegate的paint()方法中为QListView渲染QWidget

时间:2011-06-23 10:50:14

标签: qt qwidget qpainter qlistview qstyleditemdelegate

我在QListView中实现自定义窗口小部件渲染时遇到了困难。 我目前有QListView根据PlayQueue显示名为QAbstractListModel的自定义模型。

这对于简单的文本工作正常,但现在我想为每个元素显示一个自定义小部件。 所以我将QStyledItemDelegate子类化为实现paint方法,如下所示:

void QueueableDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
    if (option.state & QStyle::State_Selected)
        painter->fillRect(option.rect, option.palette.highlight());
    QWidget *widget = new QPushButton("bonjour");
    widget->render(painter);
}

正确呈现选择背景但不显示小部件。我尝试使用Qt示例中的简单QPainter命令,这很好用:

void QueueableDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
    if (option.state & QStyle::State_Selected)
        painter->fillRect(option.rect, option.palette.highlight());
    if (option.state & QStyle::State_Selected)
        painter->setPen(option.palette.highlightedText().color());
    painter->setFont(QFont("Arial", 10));
    painter->drawText(option.rect, Qt::AlignCenter, "Custom drawing");
}

所以我尝试了一些改动:

  • QStyledItemDelegate更改为QItemDelegate
  • 在渲染
  • 周围添加painter->save()painter->restore()
  • 将窗口小部件几何设置为可用大小

但我现在有点卡住了,我在互联网上搜索了一会儿,但找不到任何我想要的例子,他们都谈论编辑小部件(这很容易)或自定义绘制控件(预定义的,如进度条)。 但在这里我真的需要一个我创建的自定义小部件,其中包含一些布局,标签和像素图。 谢谢你的帮助!

我在Ubuntu 11.04上使用Qt 4.7.3 for GCC。

3 个答案:

答案 0 :(得分:14)

只是为了完成整个画面:进一步可以找到使用委托管理QWidget作为QListView项目的代码。

我终于找到了如何使用其paint(...)方法使其在QStyledItemDelegate的子类中工作。

对于性能而言似乎比以前的解决方案更有效,但是这个语句需要通过调查setIndexWidget()对创建的QWidget做什么来验证=。

最后,这是代码:

class PackageListItemWidget: public QWidget

.....

class PackageListItemDelegate: public QStyledItemDelegate

.....

void PackageListItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index ) const
{

// here we have active painter provided by caller

// by the way - we can't use painter->save() and painter->restore()
// methods cause we have to call painter->end() method before painting
// the QWidget, and painter->end() method deletes
// the saved parameters of painter

// we have to save paint device of the provided painter to restore the painter
// after drawing QWidget
QPaintDevice* original_pdev_ptr = painter->device();

// example of simple drawing (selection) before widget
if (option.state & QStyle::State_Selected)
    painter->fillRect(option.rect, option.palette.highlight());

// creating local QWidget (that's why i think it should be fasted, cause we 
// don't touch the heap and don't deal with a QWidget except painting)
PackageListItemWidget item_widget;

// Setting some parameters for widget for example
    // spec. params
item_widget.SetPackageName(index.data(Qt::DisplayRole).toString());     
    // geometry
item_widget.setGeometry(option.rect);

// here we have to finish the painting of provided painter, cause
//     1) QWidget::render(QPainter *,...) doesn't work with provided external painter 
//          and we have to use QWidget::render(QPaintDevice *,...)
//          which creates its own painter
//     2) two painters can't work with the same QPaintDevice at the same time
painter->end(); 

// rendering of QWidget itself
item_widget.render(painter->device(), QPoint(option.rect.x(), option.rect.y()), QRegion(0, 0, option.rect.width(), option.rect.height()), QWidget::RenderFlag::DrawChildren);   

// starting (in fact just continuing) painting with external painter, provided
// by caller
painter->begin(original_pdev_ptr);  

// example of simple painting after widget
painter->drawEllipse(0,0, 10,10);   
};

答案 1 :(得分:3)

好的,我终于想出了如何做我想做的事。这是我做的:

  1. 删除委托类
  2. 在我的模型的QListView::setIndexWidget()方法中调用data()来设置小部件
  3. 通过选中QListView::indexWidget()
  4. 进行设置时,确保没有小部件
  5. 处理Qt::SizeHintRole角色以返回小部件的大小提示
  6. Qt::DisplayRole角色
  7. 返回空白QVariant

    这样我可以在QListView中显示自定义小部件,并且它们是正确延迟加载的(这就是我使用模型/视图模式的原因)。但是我没有看到如何在没有显示的情况下卸载它们,这是另一个问题。

答案 2 :(得分:0)

Here就是一个例子。 看来你需要使用QStylePainter,但这只是为了绘图,据我所知,它不像真正的按钮。

相关问题