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. Drawing a line with mouse

Drawing a line with mouse

Scheduled Pinned Locked Moved General and Desktop
12 Posts 3 Posters 15.4k Views
  • 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.
  • M Offline
    M Offline
    mahesh
    wrote on last edited by
    #1

    __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 here

         painter.drawLine(lastPoint, endPoint);
         lastPoint = endPoint;
    

    }

    @
    can some one please help me ...how to draw a line to connect two components with mouse

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      You need to describe at least what you like to see and what you see. Otherwise nobody can help.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mahesh
        wrote on last edited by
        #3

        Actually i want to connect to components with mouse by a line..

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          [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 here

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

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • K Offline
            K Offline
            koahnig
            wrote on last edited by
            #5

            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

            Vote the answer(s) that helped you to solve your issue(s)

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mahesh
              wrote on last edited by
              #6

              i want to paint that line to the scene ....how can i add that corresponding line to the scene...

              1 Reply Last reply
              0
              • K Offline
                K Offline
                koahnig
                wrote on last edited by
                #7

                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.

                Vote the answer(s) that helped you to solve your issue(s)

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  mahesh
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    koahnig
                    wrote on last edited by
                    #9

                    QGraphicsScene has "an addLine":http://developer.qt.nokia.com/doc/qt-4.8/qgraphicsscene.html#addLine

                    Vote the answer(s) that helped you to solve your issue(s)

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mahesh
                      wrote on last edited by
                      #10

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

                      1 Reply Last reply
                      0
                      • C Offline
                        C Offline
                        cincirin
                        wrote on last edited by
                        #11

                        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 ?

                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          mahesh
                          wrote on last edited by
                          #12

                          sorry i didn't saw that one i was interchanged mouse press and mouse release functions...thanks now it is drawing

                          1 Reply Last reply
                          0

                          • Login

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