如何使用 Qt Delegates 进行自定义绘画? [英] How to use Qt Delegates for custom painting?

查看:51
本文介绍了如何使用 Qt Delegates 进行自定义绘画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当一个项目拖到 QTreeWidgetItems 上时,我试图覆盖它们的绘制方式.我已经覆盖了拖动事件以将 Qt.UserRole 数据设置为 1 以用于我想要绘制的 QTreeWidgetItems.在 Item Delegate 中,我读取了 UserRole 并进行了相应的绘制.

I'm trying to override the way QTreeWidgetItems are drawn when an item is dragged over them. I've overridden the drag Events to set the Qt.UserRole data to 1 for the QTreeWidgetItems that I want to paint. In the Item Delegate, I read the UserRole and draw accordingly.

我的自定义绘画完全符合预期(即粗体线);但是,我无法弄清楚如何在不抑制所有其他绘画(即文本等)的情况下抑制标准画家为拖动所做的绘画(即小矩形).

My custom painting is showing up exactly as expected (i.e. the bolded line); however, I have not been able to figure out how to suppress the drawing done by the standard painter for Dragging (i.e. the small rectangle) without suppressing all other painting (i.e. the text, etc.).

任何想法将不胜感激.

例如

def dragMoveEvent(self, event):
    pos = event.pos()
    item = self.myTreeWidget.itemAt(pos)

    # If hovered over an item during drag, set UserRole = 1
    if item:
        index = self.myTreeWidget.indexFromItem(item)
        self.myTreeWidget.model().setData(index, 1, Qt.UserRole)

    # reset UserRole to 0 for all other indices
    for i in range(self.myTreeWidget.model().rowCount()):
        _index = self.myTreeWidget.model().index(i, 0)
        if not item or index != _index:
            self.myTreeWidget.model().setData(_index, 0, Qt.UserRole)


class MyDelegate(QStyledItemDelegate):

    def paint( self, painter, option, index ):
        QStyledItemDelegate.paint(self, painter, option, index)
        painter.save()
        data = index.model().data( index, Qt.UserRole ).toInt()
            # if UserRole = 1 draw custom line
        if data[1] and data[0] == 1:
            line = QLine( option.rect.topLeft(), option.rect.topRight() )
            painter.drawLine( line )
        painter.restore()

推荐答案

这很容易通过使用 c++ 的 qt 解决:您可以定义新样式,覆盖 drawPrimitive 方法并在通过 element 参数接收到 QStyle::PE_IndicatorItemViewItemDrop 常量时进行自定义绘画(或什么都不做).下面是一个例子:

this is easily solvable with qt using c++: you can define a new style, override drawPrimitive method and do custom painting (or just do nothing) there whenever QStyle::PE_IndicatorItemViewItemDrop constant is received via element parameter. Below is an example:

class TestStyle : public QProxyStyle
{
public:
    TestStyle(QStyle *baseStyle = 0) : QProxyStyle(baseStyle) {}

    void drawPrimitive(PrimitiveElement element, const QStyleOption *option, QPainter *painter, const QWidget *widget) const
    {
        if (element == QStyle::PE_IndicatorItemViewItemDrop)
        {
            //?? do nothing or do custom painting here
        }
        else
        {
            QProxyStyle::drawPrimitive(element, option, painter, widget);
        }
    }
};

.. 

ui->treeView->setStyle(new TestStyle(ui->treeView->style()));

现在坏消息是 pyqt 似乎对 QProxyStyle 一无所知;看起来它没有包装在那里,所以为了让它工作,你需要自己包装样式类.

now the bad news is that pyqt doesn't seem to know anything about QProxyStyle; looks like it's not wrapped there, so in order for this to work you would need to wrap style classes yourself.

另一种解决方案是创建自定义 QTreeView 后代并覆盖其 paintEvent 方法.默认实现是调用 drawTree 和paintDropIndicator;其中paintDropIndicator负责拖放指示器,drawTree渲染树项.drawTree 受到保护,可供您从您的paintEvent 调用:

another solution is to create a custom QTreeView descendant and override its paintEvent method. Default implementation is calling drawTree and paintDropIndicator; where paintDropIndicator is responsible for drag and drop indicator, and drawTree renders tree items. drawTree is protected and available for you to call from your paintEvent:

class TestTreeView : public QTreeView
{
public:
    explicit TestTreeView(QWidget *parent = 0) : QTreeView(parent) {}

    void paintEvent(QPaintEvent * event)
    {
        QPainter painter(viewport());
        drawTree(&painter, event->region());
    }
};

这应该会抑制默认的拖放指示器.如果您在将其转换为 python 时遇到问题,请告诉我.

this should suppress the default drag and drop indicator. Let me know if you're having troubles converting it to python.

希望对您有所帮助,问候

hope this helps, regards

这篇关于如何使用 Qt Delegates 进行自定义绘画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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