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. How to draw a movable line on Widgets

How to draw a movable line on Widgets

Scheduled Pinned Locked Moved General and Desktop
11 Posts 4 Posters 10.9k 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.
  • A Offline
    A Offline
    adnan
    wrote on 7 Apr 2012, 12:29 last edited by
    #1

    Is a possible to draw a movable line say on QGraphicsView. Something line displacing the line parallel to itself using mouse

    1 Reply Last reply
    0
    • ? This user is from outside of this forum
      ? This user is from outside of this forum
      Guest
      wrote on 7 Apr 2012, 13:01 last edited by
      #2

      Of course you can, just get a QLine member variable, have the method to move it, either by animation or mouse or whatever and draw that line in your paint event.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mherrero
        wrote on 7 Apr 2012, 13:17 last edited by
        #3

        "ddriver":http://qt-project.org/member/9887. Can You indicate some script of example in PySide?

        1 Reply Last reply
        0
        • ? This user is from outside of this forum
          ? This user is from outside of this forum
          Guest
          wrote on 7 Apr 2012, 14:14 last edited by
          #4

          No, sorry, I don't know Python all that well and I haven't used PySide, I am a c++ guy. Just look at the documentation, it is a single QPainter method to draw a line, and some generic programming logic to set up your line's coordinates.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            adnan
            wrote on 7 Apr 2012, 16:01 last edited by
            #5

            Thanks!
            But this does not draw on QGraphicsView widget, the images displayed in QgraphicsView hide the line, i want it to display line on top of every widget

            @void aViewer::paintEvent(QPaintEvent *)
            {
            QPen pen(Qt::black, 2, Qt::SolidLine);
            QPainter painter(this);
            painter.setPen(pen);
            painter.drawLine(line1);
            }@

            1 Reply Last reply
            0
            • ? This user is from outside of this forum
              ? This user is from outside of this forum
              Guest
              wrote on 7 Apr 2012, 16:14 last edited by
              #6

              Why don't you create the line as a QGraphicsItem and set it to be on the very top in the view, so that all other items are underneath?

              1 Reply Last reply
              0
              • A Offline
                A Offline
                adnan
                wrote on 7 Apr 2012, 17:19 last edited by
                #7

                As far as moving the line is concerned, its behaving wierdly, i don't have much experience in event handling. This is what I tried:
                @void aViewer::mousePressEvent(QMouseEvent *event)
                {
                origin = event->pos();
                line1.translate(origin);
                this->update();
                }

                void aViewer::mouseMoveEvent(QMouseEvent *event)
                {
                line1.translate(event->pos());
                this->update();
                }

                void aViewer::mouseReleaseEvent(QMouseEvent *event)
                {
                // not coded yet

                }
                @

                I want to implement a sort of drag and drop on the line, that is I drag the line and it moves with the drag and gets repositioned parallel to itself.
                The above code is just causing random parallel displacements making no sense.
                A somewhat detailed help will be highly appreciated.

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  adnan
                  wrote on 7 Apr 2012, 17:23 last edited by
                  #8

                  Actually I am implementing a crop function. I want to draw a rectangle (perhaps using four lines) on an image displayed in qgraphicsview widget. the rectangle should be resizable on its ends using mouse. Once the user finalises the part of image, the program would use copy function of QImage to extract the cropped part.

                  1 Reply Last reply
                  0
                  • ? This user is from outside of this forum
                    ? This user is from outside of this forum
                    Guest
                    wrote on 7 Apr 2012, 21:28 last edited by
                    #9

                    Why don't you try something a bit different - instead of lines create two QPoints, two points is all you need to define a rectangular area. Set the first point on mouse press, on mouse move set the second point, and in the paint event, if both points are set, draw a translucent rectangle with those two points to get visual feedback of the crop area. If you need to adjust the crop area afterwards you will have to create handles to control those two points

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on 9 Apr 2012, 10:44 last edited by
                      #10

                      If you are using the QGraphicsView framework anyway, I would just use a QGraphicLineItem that you make movable by setting the ItemIsMovable flag on it.

                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        jmoellmer
                        wrote on 10 Apr 2012, 13:12 last edited by
                        #11

                        I think one of the key things to understand is how high-level events work. Take a look at QGraphicsItem::itemChange(). If you are using QGraphicsView framework and your line items are QGraphicLineItem, then you can set the flags to selectable and movable, over-ride itemChange(), and then you can make all sorts of magic happen!

                        Hope this helps a little.

                        Jeff

                        Programs for sale...

                        Fast, Reliable, Cheap: choose two.

                        1 Reply Last reply
                        0

                        3/11

                        7 Apr 2012, 13:17

                        topic:navigator.unread, 8
                        • Login

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