在QListView中实现自定义小部件呈现时遇到了困难。我现在有一个QListView,它显示了我的自定义模型,名为基于QAbstractListModel的PlayQueue。
对于简单的文本,这很好,但是现在我想为每个元素显示一个自定义小部件。因此,我对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);
}选择背景被正确地呈现,但没有显示小部件。我尝试使用简单的QPainter命令,如Qt示例中的那样,这很好:
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");
}所以我尝试了一些改变,比如:
将小部件几何形状更改为可用大小的小部件( QItemDelegate
)的
painter->save()和painter->restore()但是我现在有点卡住了,我在网上搜索了一段时间,但是找不到任何我想做的例子,他们都在谈论编辑小部件(这要容易得多)或自定义的绘制控件(预定义的控件,比如进度条)。但是在这里,我确实需要一个我创建的自定义小部件,它包含一些布局、标签和像素映射。谢谢你的帮忙!
我在Ubuntu11.04上为GCC使用Qt 4.7.3。
发布于 2011-07-01 13:14:59
好吧,我终于想出了怎么做我想做的事。以下是我所做的:
在我的模型的QListView::indexWidget()
QListView::setIndexWidget()来设置小部件
Qt::SizeHintRole角色以返回小部件的大小提示H 212H 113为Qt::DisplayRole角色H 215>G 216返回一个空白QVariant这样,我就可以在QListView中显示我的自定义小部件,并且它们被适当地延迟加载(这就是我使用模型/视图模式的原因)。但是我看不出我怎么能在不显示的情况下卸载它们,这是另一个问题。
发布于 2013-09-24 13:43:18
为了完成整个过程:进一步可以找到使用委托将QWidget作为QListView项管理的代码。
最后,我发现了如何使用它的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);
};发布于 2011-06-23 11:01:25
Here是你的一个例子。您似乎需要使用QStylePainter,但据我所知,这只是用于绘制,它的作用并不像一个真正的按钮。
https://stackoverflow.com/questions/6452838
复制相似问题