Drawing a line with mouse
-
__hi ,i got some small problem i want to connect two elements with a line(wire)...i tried like this using mouse i want to draw the line...
@
void Scene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if (mouseEvent->button() == Qt::LeftButton) {
lastPoint = mouseEvent->pos();
lineveriable = true;
}
}void Scene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if ((mouseEvent->buttons() & Qt::LeftButton) && lineveriable)
drawLineTo(mouseEvent->pos());}
void Scene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if (mouseEvent->button() == Qt::LeftButton && lineveriable) {
drawLineTo(mouseEvent->pos());
lineveriable = false;
}}
void Scene::drawLineTo(const QPointF &endPoint)
{
QPainter painter;// may be wrong is herepainter.drawLine(lastPoint, endPoint); lastPoint = endPoint;
}
@
can some one please help me ...how to draw a line to connect two components with mouse -
[quote author="mahesh438" date="1327234224"]Actually i want to connect to components with mouse by a line..[/quote]
That is obvious. But how do you like to do it? Meaning the sequence of actions with the mouse.
One way is:- Move mouse to start point
- Press and hold (left) mouse button
- Move to end point
- Release (left) mouse button
This should draw a line from point of pressing teh mouse button to the point where mouse button is released.
Is that the sequence you like to perform?
@
void Scene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if (mouseEvent->button() == Qt::LeftButton) {
lastPoint = mouseEvent->pos();
lineveriable = true;
}
}void Scene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if ((mouseEvent->buttons() & Qt::LeftButton) && lineveriable)
moveTo(mouseEvent->pos());}
void Scene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if (mouseEvent->button() == Qt::LeftButton && lineveriable) {
drawLineTo(mouseEvent->pos());
lineveriable = false;
}}
void Scene::drawLineTo(const QPointF &endPoint)
{
QPainter painter;// may be wrong is herepainter.drawLine(lastPoint, endPoint); lastPoint = endPoint;
}
void Scene::moveTo(const QPointF &endPoint)
{
lastPoint = endPoint;
}@
[Edit] the painter handling does not seem to be right as you already anticipated.
-
You need to tell the painter where to paint.
Have a look at "this":http://developer.qt.nokia.com/doc/qt-4.8/qpainter.html#QPainter-2 -
Check out what your Scene is based on ( see "this":http://developer.qt.nokia.com/doc/qt-4.8/qpaintdevice.html#details ).
If it is derived from one of these classes than you should be fine with:
@
void Scene::drawLineTo(const QPointF &endPoint)
{
QPainter painter ( this );painter.drawLine(lastPoint, endPoint); lastPoint = endPoint;
}
@
Please read also the information provided in those links. -
QGraphicsScene has "an addLine":http://developer.qt.nokia.com/doc/qt-4.8/qgraphicsscene.html#addLine
-
@
void Scene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if (mouseEvent->button() == Qt::LeftButton)
{
lastPoint = mouseEvent->scenePos();
line = new QGraphicsLineItem(QLineF(mouseEvent->scenePos(), mouseEvent->scenePos()));
addItem(line);
lineveriable = true;
}}
void Scene::mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if ((mouseEvent->buttons() & Qt::LeftButton) && lineveriable){
QLineF newLine(lastPoint, mouseEvent->scenePos());
line->setLine(newLine);
}
}void Scene::mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent)
{
if (mouseEvent->button() == Qt::LeftButton && lineveriable) {
QLineF newLine(lastPoint, mouseEvent->scenePos());
line->setLine(newLine);
lineveriable = false;
}}
@
now also it is not adding...no errors but program is unexpectedly finishing.