Results 1 to 3 of 3

Thread: How to add multiple line using qpainter

  1. #1
    Join Date
    Jul 2011
    Posts
    81
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default How to add multiple line using qpainter

    i want to draw 12separate line over a grid. using mouse clicks.
    When 1 click on over the click a point is to be shown then for the next click a line should be drawn to the current point from the first point. On third click another point is to be created. and on fourth click a line should appear from the third point to the fourth point.
    I used the code as.

    constructor:- i=0;

    void MainWindow::mousePressEvent(QMouseEvent *e)
    {
    if(e->button()==Qt::LeftButton)
    {
    point = e->pos();
    if(k==0)
    {
    point2=point;
    k=1;
    update();
    return;
    }
    if(k==1)
    k=0;
    update();
    }
    }
    void MainWindow:aintEvent(QPaintEvent *e)
    {
    QPainter painter(this);
    QPen linepen(Qt::red);
    linepen.setWidth(25);
    linepen.setCapStyle(Qt::RoundCap);
    painter.setRenderHint(QPainter::Antialiasing,true) ;
    painter.setPen(linepen);
    if(k==1)
    painter.drawPoint(point);
    else
    painter.drawLine(point,point2);
    }




    the line is drawn correctly but on the third click the first line will disappear. so i can draw only a single line. give me a solution thanks in advance

  2. #2
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to add multiple line using qpainter

    Hi,
    Now you remember only 2 points where the user clicks. You have to remember all 4 points where the user clicks and draw the line between the points that were clicked.

    You might want to learn about arrays too.

    Regards,
    Marc

  3. #3
    Join Date
    Jul 2011
    Posts
    81
    Thanks
    8
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows Symbian S60 Maemo/MeeGo

    Default Re: How to add multiple line using qpainter

    k, I will try

Similar Threads

  1. Draw line on image using QPainter
    By Qt Coder in forum Qt Programming
    Replies: 29
    Last Post: 11th August 2015, 12:09
  2. qmake: how to supply multiple variables via command-line?
    By eyeofhell in forum Qt Programming
    Replies: 1
    Last Post: 6th July 2014, 02:58
  3. Replies: 0
    Last Post: 4th August 2011, 11:11
  4. How to draw a line with tiled images with QPainter?
    By MadBear in forum Qt Programming
    Replies: 5
    Last Post: 25th July 2011, 06:36
  5. Drawing multiple line segments
    By twells5 in forum Qt Programming
    Replies: 8
    Last Post: 3rd June 2011, 14:23

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
  •  
Qt is a trademark of The Qt Company.