PDA

View Full Version : About QGraphicsItem question



jiaorenjie
15th March 2011, 03:27
when QGraphicsItem selected in QGraphicsScene, its bound will show in dashline.
How can i change this property?
i don't want to show dashline when QGraphicsItem selected.

stampede
16th March 2011, 09:38
I think you'll need to create a subclass and reimplement virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) to draw the selection yourself. In order to prevent rendering of the default selection rect, you'll need to clear the QStyle::State_Selected flag from option argument:


// header, QGraphicsItemBaseClass should be the one you want to subclass (QGraphicsPixmapItem,QGraphicsEllipseItem,...)

class MyGraphicsItem:public QGraphicsItemBaseClass{
public:
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
//...

// .cpp
void MyGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
QStyleOptionGraphicsItem opt = *option;
opt.state &= ~QStyle::State_Selected; // clears the default selection marker
QGraphicsItemBaseClass::paint(painter,&opt,widget); // render graphics item without selection rect
//...
}

You can test for the QStyle::State_Selected flag in paint method to render the selection your way:


void MyGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
if( option->state & QStyle::State_Selected ){
//... your selection rendering method here
}
}