Results 1 to 7 of 7

Thread: Draw a line with draggable

  1. #1
    Join Date
    Aug 2015
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Draw a line with draggable

    Hi to all,
    I am trying to drag a line in QPainter using mouse event. It is working in without any label and etc. But I am trying to Draw a line over the image with drag able. The line is not update. I attached my code. Please help me to drag a line over the image.
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. label1 = new QLabel(this);
    7. QPixmap pm1("download.jpg");
    8. label1->setPixmap(pm1);
    9. }
    10.  
    11. void MainWindow :: mouseMoveEvent(QMouseEvent *e)
    12. {
    13.  
    14. mFirstX = e->x();
    15. mFirstY = e->y();
    16. this->update();
    17.  
    18. }
    19.  
    20. void MainWindow :: paintEvent(QPaintEvent * e)
    21. {
    22. QImage tmp(label1->pixmap()->toImage());
    23. QPainter painter(&tmp);
    24. QPen paintpen(Qt::red);
    25. painter.setPen(paintpen);
    26. painter.drawLine(0,mFirstY,500,mFirstY);
    27. label1->setPixmap(QPixmap::fromImage(tmp));
    28. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 12th August 2015 at 07:27. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Draw a line with draggable

    If you want to move a line, not repeatedly add a line, you'll have to start with the clean image every time you draw.

    Keep the loaded image as a member of the widget in which you implement your mouse handling.
    Then, when you need to, draw the line on a copy of that image and then draw that image (or let it be drawn by QLabel).

    Instead of hacking things into a QMainWindow derived class, I would suggest to do a custom widget:

    - derive from QWidget
    - add a constructor argument that takes the file name
    - in the constructor load the image and keep it as a member
    - in sizeHint() you return the size of the image
    - in paintEvent() you draw the image and then the line
    - in mouseMoveEvent() you change the stored coordinates and call update()

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    cskumar (12th August 2015)

  4. #3
    Join Date
    Aug 2015
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Draw a line with draggable

    Hi,
    I drawn two lines over the image (i.e.) Horizontal and vertical. I can able to drag a lines. But I need to drag a line separately with highlight.

    My code is,
    Qt Code:
    1. void Widget :: paintEvent(QPaintEvent * )
    2. {
    3. QImage tmp(label1->pixmap()->toImage());
    4. QPainter painter(&tmp);
    5. QPen paintpen(Qt::black);
    6. paintpen.setWidth(1);
    7. painter.setPen(paintpen);
    8. painter.drawLine(0,mFirstY,500,mFirstY);
    9.  
    10. QPen paintpen1(Qt::red);
    11. paintpen1.setWidth(1);
    12. painter.setPen(paintpen1);
    13. painter.drawLine(mFirstX,0,mFirstX,200);
    14.  
    15. label1->setPixmap(QPixmap::fromImage(tmp));
    16. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 12th August 2015 at 12:05. Reason: missing [code] tags

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Draw a line with draggable

    I don't understand what you mean.
    You are still drawing into the same image over and over again.

    Cheers,
    _

  6. #5
    Join Date
    Aug 2015
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Draw a line with draggable

    hi,
    i had drawn lines over image as Vertical and horizontal lines (i.e ) x -axis and y-axis and to drag the line using mouse event as per your idea , in this case while i dragging the line of Vertical means it as moving the horizontal line also and this the problem had faced ,Actually i need to drag the lines separately
    Attached Images Attached Images

  7. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Draw a line with draggable

    I would suggest you make dragging one line work before trying to attempt to do two.

    Once you have one, the other one is trivial, you just need to decide how you want to distinguish between the two, e.g. direction of mouse movement or which line is closer to the point where the mouse press occurs, etc.

    Cheers,
    _

  8. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Draw a line with draggable

    Why aren't you using QRubberBand for this? That's what it is designed for.

Similar Threads

  1. To draw a line
    By vinayaka in forum Qt Quick
    Replies: 1
    Last Post: 6th June 2011, 10:53
  2. Draw Line
    By sagirahmed in forum Newbie
    Replies: 5
    Last Post: 18th October 2010, 07:49
  3. Draw a line
    By Daan in forum KDE Forum
    Replies: 1
    Last Post: 27th August 2009, 17:29
  4. Draw Line
    By aloysiusjegan in forum Qwt
    Replies: 4
    Last Post: 12th February 2009, 11:02
  5. Best way to draw a Line
    By JimDaniel in forum Qt Programming
    Replies: 1
    Last Post: 19th January 2008, 09:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.