PDA

View Full Version : QGraphicsWidget shape only works down right from center



amialobyctakpieknie
7th February 2013, 21:55
Hello,
I have made custom graphics widget with:



QRectF graphicsTask::boundingRect()
{
return QRectF(-50, -50, 100, 100);
}

QPainterPath graphicsTask::shape()
{
QPainterPath path;
path.addEllipse(boundingRect());
return path;
}


but it gets mouse events only on part of shape down and right of center of the item (bottom right quarter of a circle).
How can I make it work on all shape?

d_stranz
7th February 2013, 22:01
Move your ellipse up and to the left by 50 each:


path.addEllipse( QRectF( 0, 0, 100, 100 ) );

wysota
8th February 2013, 08:52
Move your ellipse up and to the left by 50 each:


path.addEllipse( QRectF( 0, 0, 100, 100 ) );

Hmmm.... why so?

This works perfectly fine:


#include <QtGui>

class Item : public QGraphicsItem {
public:
Item(QGraphicsItem *parent = 0) : QGraphicsItem(parent) {}
QRectF boundingRect() const { return QRectF(-50, -50, 100, 100); }
QPainterPath shape() const { QPainterPath p; p.addEllipse(boundingRect()); return p; }
void paint(QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *widget = 0) {
p->drawEllipse(boundingRect());
}
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *e) {
qDebug() << Q_FUNC_INFO;
}
};

int main(int argc, char **argv) {
QApplication app(argc, argv);
QGraphicsScene sc;
QGraphicsView view;
view.setScene(&sc);
sc.addItem(new Item);
view.show();
return app.exec();
}