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

Alessandro Portale Alessandro.Portale at trolltech.com
Sun Jun 14 18:30:07 CEST 2009


I would not assume that there happens one paintEvent() per mouseMoveEvent().

update(), only schedules a paintEvent() call for "later". If You have 
your mouse events in a high frequency, multiple scheduled paint events 
may accumulate in the queue. And instead of executing all of them, Qt 
compresses them to one single paint event.

Because of that, You should not only remember one singe of coordinates. 
Try using a QPolygon, which you add points to in the mouse event and 
which you use and clear when pointing. drawPoints() takes a QPolygon as 
parameter.

Alessandro

varun singh schrieb:
> 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/
> 
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest




More information about the Qt-interest-old mailing list