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 -
You need to describe at least what you like to see and what you see. Otherwise nobody can help.
-
Actually i want to connect to components with mouse by a line..
-
[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 -
i want to paint that line to the scene ....how can i add that corresponding line to the scene...
-
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. -
if i given "QPainter painter(this);"..this scene class i derived from --QGraphicsscene
it showing error
no matching function call QPainter ::Qpainter(scene* const)
i have one doubt is it right way to paint on the scene -
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. -
From what I see in your code is normal that you don't see any line. Also when you move the mouse (with one button clicked) you construct a new empty line ( with p1 == p2) for every new mouse movement. Is this what you want ?
-
sorry i didn't saw that one i was interchanged mouse press and mouse release functions...thanks now it is drawing