Void qwidget: repaint (int x, int y, int W, int H, bool erase = true) [Slot]
Call paintevent () immediately to re-draw the widget. If erase is true, QT erased the area (X, Y, W, h) before paintevent () is called ).
If W is a negative number, it is replaced by width ()-X, and if H is a negative number, it is replaced by height ()-y. If you need to re-draw immediately, we recommend that you use repaint (),
For example, during an animation. In most cases, update () is better because it allows QT to optimize the speed and prevent flickering.
Warning if you call repaint () in a function and it is called by paintevent (), you may see a wireless loop.
The Update () function never generates a loop.
Void qwidget: Update () [Slot]
Update the widget. When QT returns to the main event, it plans the draw event to be processed. This allows QT to be optimized to get faster speed and
Less flickering. The results of several Update () calls are usually only one paintevent () call. Qt usually erased
Region, which is not available only when the wrepaintnoerase widget flag is set.
In this difference, the key point is that repaint () calls paintevent () immediately, while Update () calls paintevent () only after several executions ().
In this way, update () will result in the following results: if the task in paintevent () is not completed, more and more tasks will be accumulated in update (). paintevent.
Program example:
(1) When the problem occurs (Update () is used every 10 milliseconds (). Paintevent () has accumulated many processing tasks ):
# Include <qpainter>
# Include <qdebug>
# Include <qmessagebox>
# Include "mainwindow. H"
# Include "ui_main1_1_h"
Mainwindow: mainwindow (qwidget * parent ):
Qmainwindow (parent ),
UI (new UI: mainwindow)
{
UI-> setupui (this );
This-> showmaximized ();
I = 0;
Realwidth = This-> width ();
Realheight = This-> height ();
Pixmap = qpixmap (realwidth, realheight );
Connect (this, signal (havedata (qpoint), this, slot (getpointanddraw (qpoint )));
Connect (& timer, signal (timeout (), this, slot (getpoint ()));
Timer. Start (10 );
}
Mainwindow ::~ Mainwindow ()
{
Delete UI;
}
Void mainwindow: getpoint ()
{
If (I <realwidth)
{
Point = qpoint (I, (uint (qrand () % realheight );
I ++;
}
Else
{
I = I % realwidth;
Point = qpoint (I, (uint (qrand () % realheight );
I ++;
}
Emit havedata (point );
}
Void mainwindow: getpointanddraw (qpoint point)
{
Index = point. X ();
Qpainter painter (& pixmap );
Painter. setpen (QT: Green );
Painter. drawline (lastpoint, point );
Painter. setpen (QT: Black );
Painter. setbrush (QT: Red );
Painter. drawrect (index + 1, 0, 5, realheight );
If (point. X () <realWidth-1)
Lastpoint = point;
Else
Lastpoint = qpoint (0, 0 );
Update ();
// This-> repaint (index-1, 0, 5, realheight );
}
Void mainwindow: paintevent (qpaintevent * E)
{
// Return;
Qpainter painter (this );
Qrect target1 (0, 0, realwidth, realheight/5 );
Qrect TARGET2 (0, realheight/5, realwidth, realheight/5 );
Qrect target3 (0, 2 * realheight/5, realwidth, realheight/5 );
Qrect target4 (0, 3 * realheight/5, realwidth, realheight/5 );
Qrect target5 (0, 4 * realheight/5, realwidth, realheight/5 );
Qrect source (0, 0, realwidth, realheight );
Painter. drawpixmap (target1, pixmap, source );
Painter. drawpixmap (TARGET2, pixmap, source );
Painter. drawpixmap (target3, pixmap, source );
Painter. drawpixmap (target4, pixmap, source );
Painter. drawpixmap (target5, pixmap, source );
}
Void mainwindow: resizeevent (qresizeevent * E)
{
Realwidth = This-> width ();
Realheight = This-> height ();
}
Void mainwindow: changeevent (qevent * E)
{
Qmainwindow: changeevent (E );
Switch (e-> type ()){
Case qevent: languagechange:
UI-> retranslateui (this );
Break;
Default:
Break;
}
}
(2) Refresh Every 1000 milliseconds, use Update (). There is enough time to process paintevent () in one second without accumulation.
# Include <qpainter>
# Include <qdebug>
# Include <qmessagebox>
# Include "mainwindow. H"
# Include "ui_main1_1_h"
Mainwindow: mainwindow (qwidget * parent ):
Qmainwindow (parent ),
UI (new UI: mainwindow)
{
UI-> setupui (this );
This-> showmaximized ();
I = 0;
Realwidth = This-> width ();
Realheight = This-> height ();
Pixmap = qpixmap (realwidth, realheight );
Connect (this, signal (havedata (qpoint), this, slot (getpointanddraw (qpoint )));
Connect (& timer, signal (timeout (), this, slot (getpoint ()));
Timer. Start (1000 );
}
Mainwindow ::~ Mainwindow ()
{
Delete UI;
}
Void mainwindow: getpoint ()
{
If (I <realwidth)
{
Point = qpoint (I, (uint (qrand () % realheight );
I ++;
}
Else
{
I = I % realwidth;
Point = qpoint (I, (uint (qrand () % realheight );
I ++;
}
Emit havedata (point );
}
Void mainwindow: getpointanddraw (qpoint point)
{
Index = point. X ();
Qpainter painter (& pixmap );
Painter. setpen (QT: Green );
Painter. drawline (lastpoint, point );
Painter. setpen (QT: Black );
Painter. setbrush (QT: Red );
Painter. drawrect (index + 1, 0, 5, realheight );
If (point. X () <realWidth-1)
Lastpoint = point;
Else
Lastpoint = qpoint (0, 0 );
Update ();
// This-> repaint (index-1, 0, 5, realheight );
}
Void mainwindow: paintevent (qpaintevent * E)
{
// Return;
Qpainter painter (this );
Qrect target1 (0, 0, realwidth, realheight/5 );
Qrect TARGET2 (0, realheight/5, realwidth, realheight/5 );
Qrect target3 (0, 2 * realheight/5, realwidth, realheight/5 );
Qrect target4 (0, 3 * realheight/5, realwidth, realheight/5 );
Qrect target5 (0, 4 * realheight/5, realwidth, realheight/5 );
Qrect source (0, 0, realwidth, realheight );
Painter. drawpixmap (target1, pixmap, source );
Painter. drawpixmap (TARGET2, pixmap, source );
Painter. drawpixmap (target3, pixmap, source );
Painter. drawpixmap (target4, pixmap, source );
Painter. drawpixmap (target5, pixmap, source );
}
Void mainwindow: resizeevent (qresizeevent * E)
{
Realwidth = This-> width ();
Realheight = This-> height ();
}
Void mainwindow: changeevent (qevent * E)
{
Qmainwindow: changeevent (E );
Switch (e-> type ()){
Case qevent: languagechange:
UI-> retranslateui (this );
Break;
Default:
Break;
}
}
(3) continue to improve (use repaint () Every time within 10 milliseconds (). Repaint (), paintevent (), no accumulation ).
# Include <qpainter>
# Include <qdebug>
# Include <qmessagebox>
# Include "mainwindow. H"
# Include "ui_main1_1_h"
Mainwindow: mainwindow (qwidget * parent ):
Qmainwindow (parent ),
UI (new UI: mainwindow)
{
UI-> setupui (this );
This-> showmaximized ();
I = 0;
Realwidth = This-> width ();
Realheight = This-> height ();
Pixmap = qpixmap (realwidth, realheight );
Connect (this, signal (havedata (qpoint), this, slot (getpointanddraw (qpoint )));
Connect (& timer, signal (timeout (), this, slot (getpoint ()));
Timer. Start (10 );
}
Mainwindow ::~ Mainwindow ()
{
Delete UI;
}
Void mainwindow: getpoint ()
{
If (I <realwidth)
{
Point = qpoint (I, (uint (qrand () % realheight );
I ++;
}
Else
{
I = I % realwidth;
Point = qpoint (I, (uint (qrand () % realheight );
I ++;
}
Emit havedata (point );
}
Void mainwindow: getpointanddraw (qpoint point)
{
Index = point. X ();
Qpainter painter (& pixmap );
Painter. setpen (QT: Green );
Painter. drawline (lastpoint, point );
Painter. setpen (QT: Black );
Painter. setbrush (QT: Red );
Painter. drawrect (index + 1, 0, 5, realheight );
If (point. X () <realWidth-1)
Lastpoint = point;
Else
Lastpoint = qpoint (0, 0 );
This-> repaint (index-1, 0, 5, realheight );
}
Void mainwindow: paintevent (qpaintevent * E)
{
// Return;
Qpainter painter (this );
Qrect target1 (0, 0, realwidth, realheight/5 );
Qrect TARGET2 (0, realheight/5, realwidth, realheight/5 );
Qrect target3 (0, 2 * realheight/5, realwidth, realheight/5 );
Qrect target4 (0, 3 * realheight/5, realwidth, realheight/5 );
Qrect target5 (0, 4 * realheight/5, realwidth, realheight/5 );
Qrect source (0, 0, realwidth, realheight );
Painter. drawpixmap (target1, pixmap, source );
Painter. drawpixmap (TARGET2, pixmap, source );
Painter. drawpixmap (target3, pixmap, source );
Painter. drawpixmap (target4, pixmap, source );
Painter. drawpixmap (target5, pixmap, source );
}
Void mainwindow: resizeevent (qresizeevent * E)
{
Realwidth = This-> width ();
Realheight = This-> height ();
}
Void mainwindow: changeevent (qevent * E)
{
Qmainwindow: changeevent (E );
Switch (e-> type ()){
Case qevent: languagechange:
UI-> retranslateui (this );
Break;
Default:
Break;
}
}