PDA

View Full Version : QGraphicsView : change selected rectangle style



kghose
28th July 2008, 17:08
Hi,

I like that QGraphicsView has all the basic select machinery written in, so we can have rubber band select and click select in our apps.

However, what I want to do is change the way the view indicates that the object is selected. By default it draws a dotted white rectangle along the bounding box. How can I change that?

Thanks
-K

Radagast
28th July 2008, 18:38
you should look into Qt sources to find out the answer :)
a dotted white rectangle along the bounding box is painted using internal Qt function, so you have to reimplement the paintEvent() and write there code for shape itself and for the selection box.

kghose
28th July 2008, 19:12
Hi,

I found the solution. You need to fiddle with the QStyleOptionGraphicsItem *option that is passed to the paint function.

My custom widget subclasses QGraphicsPixmapItem. I had reimplemented the paint function to call the regular paint funct and then my custom stuff. What I do now is as follows


void ThumbnailItem::paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
QStyleOptionGraphicsItem myoption = (*option);
myoption.state &= !QStyle::State_Selected;
QGraphicsPixmapItem::paint(painter, &myoption, widget);
.... my own drawing routines ....

So I trap option, unset the option.state QStyle::State_selected bit, and then pass it on. I then have my own code for indicating its selected.

http://doc.trolltech.com/3.3/qstyle.html#details
http://doc.trolltech.com/4.2/qstyleoptiongraphicsitem-members.html


-K