PDA

View Full Version : QGraphicsLineItem - selection style



stefan
7th August 2008, 16:40
hi,
i draw a line in QGraphicsScene, which can be selected and moved.

MainPlot=new QGraphicsScene(centralWidget());
MainPlot->setSceneRect(0,0,500,500);

QPen _Pen;
_Pen.setColor(Qt::red);
_Pen.setWidth(3);

QGraphicsLineItem* _Line=new QGraphicsLineItem(100,100,450,300);
_Line->setPen(_Pen);
_Line->setVisible(true);
_Line->setFlags(QGraphicsLineItem::ItemIsSelectable | QGraphicsLineItem::ItemIsMovable);
MainPlot->addItem(_Line);

ui.view->setScene(MainPlot);
//ui.view is raphicsView
But i don't want that when line is selected, a rectangular is drawn (and line is its diagonal). I want line selection like in "diagramscene" example supplied with Qt documantation (thin rectangular paralel with line), or some custom selection style. How to do that? I can't find this in diagramscene source.

Thanx

aamer4yu
7th August 2008, 17:50
Well, you are using QGraphicsLIneItem, while in the Diagram scene example, they are using QGraphicsPolygonItem for arrows.

Read the code for that example :)

stefan
7th August 2008, 18:05
Well, you are using QGraphicsLIneItem, while in the Diagram scene example, they are using QGraphicsPolygonItem for arrows.

Read the code for that example :)

yes, but Arrow object consists from a line and arrow. It looks like arrow is just drawn in paint method. Arrow class inherits QGraphicsLIneItem

class Arrow : public QGraphicsLineItem
why is selection different then in basic QGraphicsLIneItem?
what am I missing?

Benne Gesserit
7th August 2008, 22:26
In the diagram escen example they reimplement the boundingRect function .
I think by default it draws the boundingRect with a dotted line .I have checked the QGraphicsLineItem::boundingrect function ,I dont know what does it do ,but ini some cases it returns that rectangle that is in the picture .

stefan
8th August 2008, 09:07
I found a solution. It seems that paint method handles "selection display style". so it's easy to set a custom selection style to QGraphicsItem or derivatives.

void iddLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
QLineF _Line=line();
qint32 _x1=line().x1();
qint32 _y1=line().y1();
qint32 _x2=line().x2();
qint32 _y2=line().y2();

QPen _Pen;
_Pen.setColor(Qt::red);
if (isSelected()) _Pen.setWidth(3);
else _Pen.setWidth(1);
painter->setPen(_Pen);

painter->drawLine(_Line);

if (isSelected())
{
//Draw whatever you want ;)
//painter->drawRect(...)
//painter->drawEllipse(...)
}
}

Doru
29th November 2010, 09:02
Hi,
Can you please help me with QGraphicsLineItem paint() function:

I set the pen width to 1 but the line has a width grater the 1 pixel.
Do I have to change other settings?

Thanks