|
发表于 2005-12-15 18:33:15
|
显示全部楼层
见下面两个程序,一个闪,一个不闪
闪:
[code:1]
#include <qapplication.h>
#include <qwidget.h>
#include <qcolor.h>
#include <qpen.h>
#include <qpainter.h>
#include <qtimer.h>
class ShineWidget : public QWidget
{
QTimer* timer;
public:
ShineWidget( QWidget *parent=0, const char *name=0, WFlags f = 0)
:QWidget(parent,name, f)
{
timer=new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(repaint()));
timer->start(50);
}
protected:
void paintEvent ( QPaintEvent * )
{
const int lines=100;
QPainter p(this);
int w=this->width(); int h=this->height();
static int x=0;
p.setPen(QPen(QColor(0,0,255),2));
for(int i=0;i<lines;i++)
{
p.drawLine(0,h*i/lines,w,h*i/lines);
p.drawLine(w*i/lines,0,w*i/lines,h);
}
p.setPen(QPen(QColor(255,0,0),5));
p.drawRect(x,h/2,50,30);
x=x+w/100;
if(x>w)x=0;
}
};
int main( int argc, char **argv )
{
QApplication a( argc, argv );
ShineWidget w(0,0);
w.show();
a.setMainWidget(&w);
return a.exec();
}
[/code:1]
不闪:
[code:1]
#include <qapplication.h>
#include <qwidget.h>
#include <qimage.h>
#include <qpixmap.h>
#include <qbitmap.h>
#include <qfile.h>
#include <qpainter.h>
#include <qpoint.h>
#include <qpaintdevice.h>
#include <qtimer.h>
class NotShineWidget : public QWidget
{
QTimer* timer;
public:
NotShineWidget( QWidget *parent=0, const char *name=0, WFlags f = 0)
:QWidget(parent,name, f)
{
setBackgroundMode(Qt::NoBackground ); //让QT 不要清除窗口
timer=new QTimer(this); //设置 定时器, 定时 repaint();
connect(timer,SIGNAL(timeout()),this,SLOT(repaint()));
timer->start(50); // repaint() 周期 为 20 mS
}
protected:
void paintEvent ( QPaintEvent * )
{
const int lines=100;
int w=this->width(); int h=this->height();
QPixmap pm(w,h); //生成 一个 QPixmap 为Cache
pm.fill();
QPainter p1(&pm); //在 QPixmap 上作图
static int x=0;
p1.setPen(QPen(QColor(0,0,255),2));
for(int i=0;i<lines;i++)
{
p1.drawLine(0,h*i/lines,w,h*i/lines);
p1.drawLine(w*i/lines,0,w*i/lines,h);
}
p1.setPen(QPen(QColor(255,0,0),5));
p1.drawRect(x,h/2,50,30);
x=x+w/100;
if(x>w)x=0;
QPainter p(this);
p.drawPixmap ( 0,0, pm); //将 Pixmap 画到 窗口上.
}
};
int main( int argc, char **argv )
{
QApplication a( argc, argv );
NotShineWidget w(0,0);
w.show();
a.setMainWidget(&w);
return a.exec();
}
[/code:1] |
|