Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Draw a line in Scene with click mouse.
Forum Updated to NodeBB v4.3 + New Features

Draw a line in Scene with click mouse.

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 2 Posters 4.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi
    You are not allowed to use QPainter OUTSIDE of paintEvent. :)

    please see here
    http://www.walletfox.com/course/qgraphicsitemruntimedrawing.php

    AlvaroSA 1 Reply Last reply
    2
    • mrjjM mrjj

      Hi
      You are not allowed to use QPainter OUTSIDE of paintEvent. :)

      please see here
      http://www.walletfox.com/course/qgraphicsitemruntimedrawing.php

      AlvaroSA Offline
      AlvaroSA Offline
      AlvaroS
      wrote on last edited by
      #3

      @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!

      1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #4

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

        AlvaroSA 1 Reply Last reply
        2
        • mrjjM mrjj

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

          AlvaroSA Offline
          AlvaroSA Offline
          AlvaroS
          wrote on last edited by
          #5

          @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

          1 Reply Last reply
          0
          • mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #6

            @AlvaroS said:

            MainWindow.scene

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

            AlvaroSA 1 Reply Last reply
            0
            • mrjjM mrjj

              @AlvaroS said:

              MainWindow.scene

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

              AlvaroSA Offline
              AlvaroSA Offline
              AlvaroS
              wrote on last edited by
              #7

              @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);
              ^

              mrjjM 1 Reply Last reply
              0
              • AlvaroSA AlvaroS

                @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);
                ^

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #8

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

                AlvaroSA 2 Replies Last reply
                1
                • mrjjM mrjj

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

                  AlvaroSA Offline
                  AlvaroSA Offline
                  AlvaroS
                  wrote on last edited by
                  #9

                  @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

                  1 Reply Last reply
                  0
                  • mrjjM mrjj

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

                    AlvaroSA Offline
                    AlvaroSA Offline
                    AlvaroS
                    wrote on last edited by
                    #10

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

                    mrjjM 1 Reply Last reply
                    0
                    • AlvaroSA AlvaroS

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

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

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

                      1 Reply Last reply
                      1

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved