Issue
In my ProxyStyle
class, I have a drawPrimitive
function where I check if element == QtWidgets.QStyle.PE_IndicatorItemViewItemDrop
to draw a green line when the user drag and drop items in my TreeView
.
I set the action so that the item can be drop between but not over. So I would like to change the color of the highlight like this: 1- line between item green (horizontal line) 2- line over item red (this line wraps the item).
Do you have an idea of how to do it? My drawPrimitive
function have these arguments:
element(str)
option (QtGui.QStyleOptionViewItem)
painter(QtGui.QPainter)
widget (QtWidgets.QWidget)
Is there a flag like PE_IndicatorItemViewItemDrop
that indicate if an item will be drop between or over?
Solution
According to the latest in Qt's 5.11 branch on github, the test is a simple height equals 0:
case PE_IndicatorItemViewItemDrop: {
QRect rect = opt->rect;
if (opt->rect.height() == 0)
p->drawLine(rect.topLeft(), rect.topRight());
else
p->drawRect(rect);
break; }
Your code should use the same test to decide which color to use. If option.rect.height == 0
then Qt::green
else Qt::red
. Copy the current QPen and change its color, then call the base class' drawPrimitive. The following pseudocode should suffice:
lastPen = painter.pen()
myPen = QtGui.QPen(lastPen)
if (option.rect.height() == 0):
myPen.setColor(Qt.green)
else:
myPen.setColor(Qt.red)
painter.setPen(myPen)
QProxyStyle.drawPrimitive(self, element, option, painter, widget)
painter.setPen(pen)
Sorry if the code is ugly. I don't know PySide, just C++ Qt.
Answered By - walkingTarget
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.