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 active

    how can i do that?
    thanks!


  • Qt Champions 2016

    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!


  • Qt Champions 2016

    @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.php

    It creates a custom scene.
    class Scene : public QGraphicsScene

    You 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


  • Qt Champions 2016

    @AlvaroS said:

    MainWindow.scene

    hi
    why with mainwin in front ?
    If its a member of main window , just
    scene->addLine(line_traj,blue);



  • @mrjj If I do that it says me:

    scene_mouse_event.cpp:35: error: 'scene' was not declared in this scope
    scene->addLine(line_traj,blue);
    ^


  • Qt Champions 2016

    @AlvaroS

    Well its say it dont know scene
    so if you added to main wind , its pretty odd.

    IN what class / function do u call
    scene->addLine(line_traj,blue);

    If not in mainwindow, then that is why.



  • @mrjj No, I call in "scene" class because is here where I have to put mousepressevent.

    So it is here where I have to modify the scene



  • @mrjj So I need to modify scene which is declarated in mainwindow class in "scene" class.
    For that reason I have declarated scene as a public in mainwindow but it does not work.


  • Qt Champions 2016

    @AlvaroS

    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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.