[Qt-interest] Painting outside QWidget::paintEvent() on MS Windows

varun singh mailthatmale at yahoo.co.in
Sun Jun 14 16:58:54 CEST 2009


hi,
well thank you Srdjan my problem is at least partially solved now.

A new issue:
 Well I'm trying to draw a line with QWidget::mouseMoveEvent (just like it is done in a painting software).

This is the code I've prepared:
*************************************************************
/* m_xCoord and m_yCoord are members of class MyWidget */

void MyWidget::mouseMoveEvent(QMouseEvent *event)
{
    m_xCoord = event->x();
    m_yCoord = event->y();  
/* Stores the x & y co-ordinates of 
 mouse position when the event occurs. */

    update(m_xCoord, m_yCoord, 5, 5);
/* Updates (Paints) the region specified by
co-ordinates given by this event */
}

void MyWidget::paintEvent(QPaintEvent *event)
{

/* paintEvent puts a point at a place specified
by x & y co-ordinates given by mouseMoveEvent(). */   

   QPainter painter(this);
   QPen p(Qt::red, 5, Qt::SolidLine, Qt::SquareCap);
   painter.setPen(p);
   painter.begin(this);
   painter.setPen(p);
   painter.drawPoint(m_xCoord, m_yCoord);
}
*************************************************************

The issue is that it is not behaving the way it should. Theoritically, it should put a point on every position that I move over with the mouse clicked, but if I move over too quickly, all the positions are not painted, thus forming broken figures. 

I've also tried replaint() (instead of update()).

I wonder why it is happening and how can it be solved.

-Varun

> Hi,
> 
> 2009/6/13 varun singh <mailthatmale at yahoo.co.in>:
> >
> > hi,
> > I've started learning Qt on Microsoft Windows
> platform.
> >
> > Qt on Windows does not allow painting outside
> QWidget::paintEvent(). But I want to draw a pixel specified
> by the click of the mouse (which could have been done using
> QWidget::mousePressEvent(), had Qt allowed painting outside
> > QWidget::paintEvent()).
> >
> > So, how can I do that?
> 
> Make your mouse click event cause a paint event. Then paint
> in your
> paintEvent().
> 
> As in:
> 
> yourclass::mousePressEvent()
> {
>   mMessage = "Hello!";   
>    /* mMessage is a class member of type
> QString */
>   update();           
>         /* should cause a repaint */
> }
> 
> yourclass::paintEvent()
> {
>   qWarning(mMessage.toAscii());   /*
> mMessage has been modified in
> mousePressEvent() */
> }
> 
> Something like the above?
> 
> Srdjan
> 
> 



      Explore and discover exciting holidays and getaways with Yahoo! India Travel http://in.travel.yahoo.com/




More information about the Qt-interest-old mailing list