最近练习Qt,需要一个飞机在屏幕上画五边形。虽然达到的效果不是非常的理想,但是勉强还是达到了效果,欢迎大家指正。用到的飞机图片如下。
第一步:初始化,在构造函数里面,把图片向左旋转18°
// 向左旋转18°
matrix.rotate(-18);
ui.label->setPixmap(QPixmap("C:\Users\Administrator.USER-20190313RI\Desktop\img\life.png").transformed(matrix, Qt::SmoothTransformation));
第二步:在定时器里面执行如下代码
switch(flag) { case 1: { if (num == 10) { num = 0; flag = 2; matrix.rotate(72); ui.label->setPixmap(QPixmap("C:\Users\Administrator.USER-20190313RI\Desktop\img\life.png").transformed(matrix, Qt::SmoothTransformation)); } int x = ui.label->x() - 8; int y = ui.label->y() - 10; ui.label->move(x, y); num++; break; } case 2: { if (num == 10) { num = 0; flag = 3; matrix.rotate(72); ui.label->setPixmap(QPixmap("C:\Users\Administrator.USER-20190313RI\Desktop\img\life.png").transformed(matrix, Qt::SmoothTransformation)); } int x = ui.label->x() + 15; int y = ui.label->y() - 10; ui.label->move(x, y); num++; break; } case 3: { if (num == 10) { num = 0; flag = 4; matrix.rotate(72); ui.label->setPixmap(QPixmap("C:\Users\Administrator.USER-20190313RI\Desktop\img\life.png").transformed(matrix, Qt::SmoothTransformation)); } int x = ui.label->x() + 15; int y = ui.label->y() + 10; ui.label->move(x, y); num++; break; } case 4: { if (num == 10) { num = 0; flag = 5; matrix.rotate(72); ui.label->setPixmap(QPixmap("C:\Users\Administrator.USER-20190313RI\Desktop\img\life.png").transformed(matrix, Qt::SmoothTransformation)); } int x = ui.label->x() - 8; int y = ui.label->y() + 10; ui.label->move(x, y); num++; break; } case 5: { if (num == 10) { num = 0; flag = 1; matrix.rotate(72); ui.label->setPixmap(QPixmap("C:\Users\Administrator.USER-20190313RI\Desktop\img\life.png").transformed(matrix, Qt::SmoothTransformation)); } int x = ui.label->x() - 14; int y = ui.label->y(); ui.label->move(x, y); num++; break; } default: { break; } }