PDA

View Full Version : mousePressEvent QPolygon boundingRect()



Speerfish
28th January 2014, 17:32
Hello erverybody, have some problems with the mousePressevent in combination with QGraphicsSceneMouseEvent.
Everything works fine, but the mouse events work with the bounding Rect but not exclusively only the Qpolygon inside of the bounding rect.
Since i do have to rotate the Qpolygon, and other items are very close to each other (also want to click them seperatly) this is not the best practise for me...Any help is appreciated.
Since i will position the Qpolygon from its center, i wrote a small conversion funciton, (early stage, will program it out late
frame.cpp


#include "frame.h"

frame::frame(QGraphicsItem *parent)
: umbrella(parent)
{
this->frameRotation=0;
}

QRectF frame::boundingRect() const
{
return QRectF(0,0,128,128);
}

void frame::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
QPolygon myframePolygon;
QColor myframecolor;
QBrush myframeBrush;
QPainterPath myframePainterPath;
QPen myframePen;
painter->drawRect(boundingRect());
myframePen.setWidth(4);
myframePen.setColor(Qt::black);
myframePen.setJoinStyle(Qt::RoundJoin);
myframecolor=(QColor(0,0,255,70));
myframeBrush.setColor(myframecolor);
myframeBrush.setStyle(Qt::SolidPattern);
myframePolygon.append(QPoint(2,27));
myframePolygon.append(QPoint(30,1));
myframePolygon.append(QPoint(65,8));
myframePolygon.append(QPoint(100,1));
myframePolygon.append(QPoint(127,28));
myframePolygon.append(QPoint(119,64));
myframePolygon.append(QPoint(122,96));
myframePolygon.append(QPoint(99,126));
myframePolygon.append(QPoint(64,119));
myframePolygon.append(QPoint(29,125));
myframePolygon.append(QPoint(5,96));
myframePolygon.append(QPoint(9,62));
myframePainterPath.addPolygon(myframePolygon);
int frameWide =128;
int frameHigh =128;
int xc = 0 + frameWide/2;
int yc = 0 + frameHigh/2;
painter->translate(+xc,+yc);
painter->rotate(frameRotation);
painter->translate(-xc,-yc);
painter->setPen(myframePen);
painter->drawPolygon(myframePolygon);
painter->fillPath(myframePainterPath,myframeBrush);
}

void frame::mousePressEvent(QGraphicsSceneMouseEvent *event){
if(event->button() == Qt::RightButton){
qDebug() << "click with the right mouse button";
}
else if(event->button() == Qt::LeftButton){
qDebug() << "click with the left mouse button";
}
else{
qDebug() << "click with the whell button";
}
QGraphicsItem::mousePressEvent(event);
}

void frame::setFramePosMiddle(qreal x, qreal y)
{
this->setPos(x-128/2,y-128/2);
}

void frame::setFrameRotation(qreal frameRotation)
{
this->frameRotation=frameRotation;
update();
}


frame.h


#ifndef FRAME_H
#define FRAME_H

#include <QGraphicsItem>
#include <QtGui>
#include "umbrella.h"

class umbrella;
class QGraphicsSceneMouseEvent;
class QParallelAnimationGroup;

class frame : public umbrella
{
public:
frame(QGraphicsItem *parent = 0);
QRectF boundingRect() const;
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
void setFramePosMiddle(qreal x,qreal y);
void setFrameRotation(qreal frameRotation);
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
qreal x,y,frameRotation;
};

#endif // FRAME_H


Result
9989

All mouse events work, but i want that only the events work in the Qpolygon not alos in the show boundingRect()

anda_skoa
28th January 2014, 18:14
http://qt-project.org/doc/qt-4.8/qgraphicsitem.html#shape

Cheers,
_

Speerfish
29th January 2014, 07:07
Hm, how and where should i use this ?
Am i confused ? or what..

wagmare
29th January 2014, 07:21
same like boundingRect() you have to override the shape() and return the painterpath of polygon ..
ref: http://qt-project.org/doc/qt-4.8/graphicsview-elasticnodes-node-cpp.html see the implementation of shape

Speerfish
29th January 2014, 07:36
Thanks man,
just for checking it out i did the following
frame.h file


#ifndef FRAME_H
#define FRAME_H

#include <QGraphicsItem>
#include <QtGui>
#include "umbrella.h"

class umbrella;
class QGraphicsSceneMouseEvent;
class QParallelAnimationGroup;

class frame : public umbrella
{
public:
frame(QGraphicsItem *parent = 0);
QRectF boundingRect() const;
QPainterPath shape() const; /**<-- did add this line here*/
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0);
void setFramePosMiddle(qreal x,qreal y);
void setFrameRotation(qreal frameRotation);
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *event);
qreal x,y,frameRotation;
};

#endif // FRAME_H


frame.cpp file


#include "frame.h"

frame::frame(QGraphicsItem *parent)
: umbrella(parent)
{
this->frameRotation=0;
}

QRectF frame::boundingRect() const
{
return QRectF(0,0,128,128);
}

QPainterPath frame::shape() const
{
QPolygon framePolygon;
framePolygon.append(QPoint(2,27));
framePolygon.append(QPoint(30,1));
framePolygon.append(QPoint(65,8));
framePolygon.append(QPoint(100,1));
framePolygon.append(QPoint(127,28));
framePolygon.append(QPoint(119,64));
framePolygon.append(QPoint(122,96));
framePolygon.append(QPoint(99,126));
framePolygon.append(QPoint(64,119));
framePolygon.append(QPoint(29,125));
framePolygon.append(QPoint(5,96));
framePolygon.append(QPoint(9,62));
QPainterPath path;
path.addPolygon(framePolygon);
return path;
}

void frame::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
QPolygon myframePolygon;
QColor myframecolor;
QBrush myframeBrush;
QPainterPath myframePainterPath;
QPen myframePen;
painter->drawRect(boundingRect());
myframePen.setWidth(4);
myframePen.setColor(Qt::black);
myframePen.setJoinStyle(Qt::RoundJoin);
myframecolor=(QColor(0,0,255,70));
myframeBrush.setColor(myframecolor);
myframeBrush.setStyle(Qt::SolidPattern);
myframePolygon.append(QPoint(2,27));
myframePolygon.append(QPoint(30,1));
myframePolygon.append(QPoint(65,8));
myframePolygon.append(QPoint(100,1));
myframePolygon.append(QPoint(127,28));
myframePolygon.append(QPoint(119,64));
myframePolygon.append(QPoint(122,96));
myframePolygon.append(QPoint(99,126));
myframePolygon.append(QPoint(64,119));
myframePolygon.append(QPoint(29,125));
myframePolygon.append(QPoint(5,96));
myframePolygon.append(QPoint(9,62));
myframePainterPath.addPolygon(myframePolygon);
int frameWide =128;
int frameHigh =128;
int xc = 0 + frameWide/2;
int yc = 0 + frameHigh/2;
painter->translate(+xc,+yc);
painter->rotate(frameRotation);
painter->translate(-xc,-yc);
painter->setPen(myframePen);
painter->drawPolygon(myframePolygon);
painter->fillPath(myframePainterPath,myframeBrush);
}

void frame::mousePressEvent(QGraphicsSceneMouseEvent *event){
if(event->button() == Qt::RightButton){
qDebug() << "click with the right mouse button";
}
else if(event->button() == Qt::LeftButton){
qDebug() << "click with the left mouse button";
}
else{
qDebug() << "click with the whell button";
}
QGraphicsItem::mousePressEvent(event);
}

void frame::setFramePosMiddle(qreal x, qreal y)
{
this->setPos(x-128/2,y-128/2);
}

void frame::setFrameRotation(qreal frameRotation)
{
this->frameRotation=frameRotation;
update();
}



Works fine now, but if i rotate the polygon again....it does not also turn the changed bounding rect to the angle.....
which results that there are 2 Polygons, one which is turned and the one which the system referes.
clicking the mouse works with the one from the system....which does not overlay with the rotated one...

I am so close to get that done to.....any idea to fix this little thing now.
Maybe there is evern another function to rotate the polygon and the system bounding rect in one piece....

Thanks for the link

anda_skoa
29th January 2014, 10:07
Works fine now, but if i rotate the polygon again....it does not also turn the changed bounding rect to the angle.....

Because you only rotate the painting of the polygon.
If the polygin is your shape, then you need to rotate the polygon and use the same values for shape and drawing.

Btw, why not rotate the item?

Cheers,
_