返回介绍

A Rectangle Draw "Benchmark"

发布于 2019-10-04 14:57:57 字数 3907 浏览 1067 评论 0 收藏 0

This example continuously draws rectangles in a window and has another widget that counts the number of rectangles that are drawn per second.


Header file:

/****************************************************************************
** $Id:  qt/forever.h   3.0.5   edited Oct 12 2001 $
**
** Definition of something or other
**
** Created : 979899
**
** Copyright (C) 1997 by Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#ifndef FOREVER_H
#define FOREVER_H

#include <qwidget.h>


const int numColors = 120;


class Forever : public QWidget
{
    Q_OBJECT
public:
    Forever( QWidget *parent=0, const char *name=0 );
protected:
    void        paintEvent( QPaintEvent * );
    void        timerEvent( QTimerEvent * );
private slots:
    void        updateCaption();
private:
    int         rectangles;
    QColor      colors[numColors];
};


#endif


Implementation:

/****************************************************************************
** $Id:  qt/forever.cpp   3.0.5   edited Oct 12 2001 $
**
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
**
** This file is part of an example program for Qt.  This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/

#include <qtimer.h>
#include <qpainter.h>
#include <qapplication.h>
#include <stdlib.h>                             // defines rand() function

#include "forever.h"


//
// Forever - a widget that draws rectangles forever.
//

//
// Constructs a Forever widget.
//

Forever::Forever( QWidget *parent, const char *name )
    : QWidget( parent, name )
{
    for (int a=0; a<numColors; a++) {
        colors[a] = QColor( rand()&255,
                            rand()&255,
                            rand()&255 );
    }
    rectangles = 0;
    startTimer( 0 );                            // run continuous timer
    QTimer * counter = new QTimer( this );
    connect( counter, SIGNAL(timeout()),
             this, SLOT(updateCaption()) );
    counter->start( 1000 );
}


void Forever::updateCaption()
{
    QString s;
    s.sprintf( "Qt Example - Forever - %d rectangles/second", rectangles );
    rectangles = 0;
    setCaption( s );
}


//
// Handles paint events for the Forever widget.
//

void Forever::paintEvent( QPaintEvent * )
{
    QPainter paint( this );                     // painter object
    int w = width();
    int h = height();
    if(w <= 0 || h <= 0)
        return;
    paint.setPen( NoPen );                      // do not draw outline
    paint.setBrush( colors[rand() % numColors]);// set random brush color

    QPoint p1( rand()%w, rand()%h );    // p1 = top left
    QPoint p2( rand()%w, rand()%h );    // p2 = bottom right

    QRect r( p1, p2 );
    paint.drawRect( r );                        // draw filled rectangle
}

//
// Handles timer events for the Forever widget.
//

void Forever::timerEvent( QTimerEvent * )
{
    for ( int i=0; i<100; i++ ) {
        repaint( FALSE );                       // repaint, don't erase
        rectangles++;
    }
}


//
// Create and display Forever widget.
//

int main( int argc, char **argv )
{
    QApplication a( argc, argv );               // create application object
    Forever always;                             // create widget
    a.setMainWidget( &always );                 // set as main widget
    always.setCaption("Qt Example - Forever");
    always.show();                              // show widget
    return a.exec();                            // run event loop
}

See also Examples.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文