Draw a line in Scene with click mouse.
-
Hello everyone:
First of all thanks a lot for reading this post and being able to help.
I would like to draw a line in a Scene.
User click on the Start point and on End point of the line.i do this:
void MainWindow::mousePressEvent(QMouseEvent * e) { if (generate_traj){ number_click_press++; QPointF pt = e->pos(); /*Paint in color each head arrow line*/ QPainter painter1(this); QPen blue(Qt::blue); //Draw lines in blue blue.setCosmetic(true); //Users can select easier the lines. blue.setWidthF(0.3); //Lines width blue.setBrush(Qt::blue); painter1.setPen(blue); /*Fill brush each head arrow*/ /*Add head arrow to the scene*/ if (number_click_press>2){ QLineF line_traj; line_traj.setP1(last_press); line_traj.setP2(pt); scene->addLine(line_traj,blue); } last_press=pt; } }
if does not give me any errors. But line is not painted...
And when I do click on the scene it says me this:
QWidget::paintEngine: Should no longer be called
QPainter::begin: Paint device returned engine == 0, type: 1
QPainter::setPen: Painter not activehow can i do that?
thanks! -
Hi
You are not allowed to use QPainter OUTSIDE of paintEvent. :)please see here
http://www.walletfox.com/course/qgraphicsitemruntimedrawing.php -
@mrjj hello, first of all thanks a lot for answering!
My code looks like this:
.CPP
void MainWindow::mousePressEvent(QGraphicsSceneMouseEvent *event) { qDebug()<<"in musubclass mouse press event: "; if (generate_traj){ number_click_press++; QPointF pt = event->scenePos(); qDebug()<<"in musubclass mouse press event: "<< pt; /*Paint in color each head arrow line*/ QPen blue(Qt::blue); //Draw lines in blue blue.setCosmetic(true); //Users can select easier the lines. blue.setWidthF(0.3); //Lines width blue.setBrush(Qt::blue); if (number_click_press>=2){ QLineF line_traj; line_traj.setP1(last_press); line_traj.setP2(pt); scene->addLine(line_traj,blue); } last_press=pt; } }
Header:
protected: void mousePressEvent(QGraphicsSceneMouseEvent *event);
The problem is that code never enters in that function....
However if i write QMouseEvent instead of QGraphicsSceneMouseEvent it enters but I need just that it enters if user click on the scene, and with QMouseEvent it enters if user clicks wherever.
Wha t am i doing wrong?
Thanks a lot!
-
@AlvaroS said:
MainWindow::mousePressEvent
You override it for main window?
Normally you would use QGraphicsScene or QGraphicsView since
its the object u click on.Also for mainwindow, I doubt it would use
QGraphicsSceneMouseEvent as that is for QGraphicsView.Please see link. it does exactly what you want.
http://www.walletfox.com/course/qgraphicsitemruntimedrawing.phpIt creates a custom scene.
class Scene : public QGraphicsSceneYou can just use that class and then you can draw lines.
-
@mrjj Thanks a lot for answering me.
I have just done what example says.
But now I have a problem to modify the scene in "scene" class.
The scene is declarated in mainwindow class as a public.
So when I try to modify the scene in "scene" class like:MainWindow.scene->addLine(line_traj,blue);
it says me this error:
/scene_mouse_event.cpp:35: error: expected unqualified-id before '.' token
MainWindow.scene->addLine(line_traj,blue);
^Any ideas?
thanks a lot
-
Well, why do u need to modify scene from outside ?
and from what class do you try this?Why cant you just do it in main window? then ?
Something seems off. :)
You would need a pointer to the mainwindow in that other
class to do it but that
is not good style.
You should give the other class the scene as pointer then.