QT 4.5 - Changing the selection marquee for QGraphicsItem
Asked Answered
Z

3

8

Is it possible to define the style of the selection marquee when a QGraphicsItem is set selectd via setSelected(true)?

Zemstvo answered 22/10, 2009 at 4:17 Comment(0)
B
5

There's a good solution at:

http://www.qtcentre.org/threads/15089-QGraphicsView-change-selected-rectangle-style

Barbarity answered 30/7, 2010 at 20:12 Comment(1)
Note to anyone finding this: This new answer is what you want; a much better answer than the earlier accepted answer.Domingadomingo
L
5

Technically no.

After a quick look into Qt's source code, it seems that the standard item types QGraphics*Item have the dashed outline as a default behavior. You can work around this by subclassing your own QGraphicsItem.

if (option->state & (QStyle::State_Selected | QStyle::State_HasFocus))
    qt_graphicsItem_highlightSelected(this, painter, option);

I guess you can not change the "style". This seems to be a very static feature of the standard QGraphicsItems.

Hope that helps!

Lanell answered 16/11, 2009 at 18:42 Comment(1)
@Barbarity actually found a great workaround, see other answer.Domingadomingo
B
5

There's a good solution at:

http://www.qtcentre.org/threads/15089-QGraphicsView-change-selected-rectangle-style

Barbarity answered 30/7, 2010 at 20:12 Comment(1)
Note to anyone finding this: This new answer is what you want; a much better answer than the earlier accepted answer.Domingadomingo
A
0

Just adding to cesarbs' answer with a python example, it took me a bit to translate all the syntax so thought I'd provide some code:

class CustomItem(QtWidgets.QGraphicsPixmapItem):
    def paint(self, painter, option, widget):
        if option.state & QtWidgets.QStyle.State_Selected:
            option.state &= not QtWidgets.QStyle.State_Selected
            super().paint(painter, option, widget)
            
            # draw red outline for example
            pen = QtGui.QPen(QtGui.QColor("red"))
            painter.setPen(pen)
            painter.drawRect(option.rect)
        else:
            super().paint(painter, option, widget)
Alethiaaletta answered 3/2, 2022 at 1:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.