Showing of drawn rectangles
-
How to make showing of rectangles while they are drawing in Qt?
My scene of the drawing is as follows:
@#include <QtGui/QMainWindow>
#include "ui_rectangleview.h"
#include "rectangleview.h"
class Scene : public QGraphicsScene
{
private:
int X, Y;
QGraphicsView *Parent;
public:Scene(QGraphicsView *parent) : QGraphicsScene(parent)
{
Parent = parent;
addLine(-250,0,250,0,QPen(Qt::red));
addLine(0,-230,0,250,QPen(Qt::red));
}
void mousePressEvent(QGraphicsSceneMouseEvent *e)
{
X = e->scenePos().x();
Y = e->scenePos().y();
}
void mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
{
QGraphicsItem *item = 0;QRectF r(X, Y, e->scenePos().x()-X, e->scenePos().y()-Y);
addRect(r, QPen(Qt::black),Qt::NoBrush);//Parent->mapToScene(e->pos()); //Parent->setFlag(QGraphicsItem::ItemIsMovable);
}
};@Rectangles are drawn when you release the mouse button. I want rectangles to be shown when moving the mouse. Please help.
-
QGraphicsScene::mouseMoveEvent() ??? But I've never do this...Look at classes: "QGraphicsWidget":http://doc.trolltech.com/4.7-snapshot/qgraphicswidget.html, "QGraphicsItem":http://doc.trolltech.com/4.7-snapshot/qgraphicsitem.html
-
What shoud I put in QGraphicsScene::mouseMoveEvent() function (in order that rectangle is drawn temporary, not permanently added to scene)?
-
bq. Rectangles are drawn when you release the mouse button. I want rectangles to be shown when moving the mouse.
Your last sentence is quite unlogical...
If you want to see anything, you have to draw it. If you want to draw, when you move your mouse, use that event as I mentioned above. On painting it uses paintEvent...but...
You are not allowed to draw in QGraphicsScene, because it's just map (what come where, and what will stay at top, and so on...), it's not a widget, you have to draw in @void QGraphicsItem::paint()@ or in @QGraphicsView::paintEvent(...)@ , which gives painter to all used objects/items (QGraphicsItem through paint() method)
My advice is: You shouldn't work in QGraphicsScene but in QGraphicsView, it'll be better...And even better will be, to work in QGraphicsItem...
Best wishes...