×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    29

    Question cannot draw cursor trace

    Hi! I want to implement a drawing pad, with qt, so I write the code like this:
    void QImagePad::mousePressEvent(QMouseEvent *event)
    {
    if(QEvent::MouseButtonPress == event->type()
    {
    prePoint=event->pos();
    curPoint=event->pos();
    }
    }

    void QImagePad::mouseMoveEvent(QMouseEvent *event)
    {
    if(event->buttons()&Qt::LeftButton)
    {
    drag=true;
    prePoint=curPoint;
    curPoint=event->pos();
    update();
    }
    }

    void QImagePad:aintEvent(QPaintEvent *event)
    {
    QPainter painter(this);
    painter.setPen( QPen(Qt::black, 5) );
    if(drag)
    {
    painter.drawLine(prePoint, curPoint);
    }

    }

    I thought with these three functions, cursor trace can be drawed. However, in fact, only line between the latest two cursor points is drawed. I don't know the reason, and hope someone can help me, thanks.!

  2. #2
    Registered User
    Join Date
    Dec 2007
    Location
    Oslo
    Posts
    12

    Re: cannot draw cursor trace

    By calling update() with no arguments you are forcing your entire widget to be marked as dirty and therefore repainted. The reason it only draws the last line segment is because that's the only line segment you are telling it to draw. You are only calling "drawLine" once inside the paintEvent() so it only draws one line.

    Consider calling update() with only the dirty rectangle or draw all of the line segments each time instead of only the last one. You can use the QPaintEvent::rect() function to see exactly which parts of the widget need repainting in case you don't really want to paint everything again.

    There is already an example in Qt that does exactly what you want so you can use it as a guide:

    http://doc.trolltech.com/4.5/widgets-scribble.html

Similar Threads

  1. View server panic with 11 on draw
    By raj_rr7 in forum Symbian User Interface
    Replies: 7
    Last Post: 2009-05-18, 11:36
  2. Draw function
    By praveenp in forum Symbian User Interface
    Replies: 4
    Last Post: 2008-10-17, 12:27
  3. why can i not draw line in Draw()?
    By isarc in forum Symbian
    Replies: 7
    Last Post: 2008-05-13, 10:51
  4. Replies: 0
    Last Post: 2008-04-15, 08:38
  5. Draw lines into EDWIN like in SMS app
    By stenlik in forum Symbian User Interface
    Replies: 6
    Last Post: 2006-11-30, 09:04

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×