首先上主要代码:
#include "mainwindow.h"
#include <QtGui>
MainWindow::MainWindow(QWidget *parent)
: QWidget(parent),rot(0)
{
setFixedSize(1020,620);
timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(update()));
timer->start(20);
}
MainWindow::~MainWindow()
{
}
void MainWindow::paintEvent(QPaintEvent *)
{
rot += 2;
QPainter painter(this);
int k=-1;
painter.setWindow(-110,-110,1020,620);
painter.setRenderHint(QPainter::Antialiasing,true);
QMatrix matrix;
for(int i=10;i<420;i+=200)
for(int j=10;j<820;j+=200,k=-k){
matrix.reset();
matrix.translate(i,j);
matrix.rotate(rot*k);
painter.setWorldMatrix(matrix);
painter.setBrush(QBrush(Qt::black,Qt::SolidPattern));
painter.setPen(QPen(Qt::red,0,Qt::NoPen,Qt::RoundCap));
painter.drawPie(-100,-100,200,200,93*16,180*16);
painter.setBrush(QBrush(Qt::white,Qt::SolidPattern));
painter.drawPie(-100,-100,200,200,273*16,180*16);
painter.drawPie(-50,-100,100,100,90*16,180*16);
painter.setBrush(QBrush(Qt::black,Qt::SolidPattern));
painter.drawPie(-50,0,100,100,270*16,180*16);
painter.drawEllipse(-20,-70,20,20);
painter.setBrush(QBrush(Qt::white,Qt::SolidPattern));
painter.drawEllipse(-20,30,20,20);
}
}
void MainWindow::mousePressEvent(QMouseEvent *)
{
if(timer->isActive())
timer->stop();
else
timer->start(20);
}
这是两个嵌套的for循环,目的是画3x5=15个一样的图形,但是,请看截图:
仅仅出现九个。右边没显示。为什么会这样?
附上工程源码