给我份代码,QT画一个旋转的矩形
时间: 2023-04-02 21:04:12 浏览: 177
qt 画旋转矩形
好的,以下是代码:
#include <QPainter>
#include <QTimer>
#include <QWidget>
class Widget : public QWidget {
public:
Widget(QWidget *parent = nullptr) : QWidget(parent) {
setFixedSize(400, 400);
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, QOverload<>::of(&Widget::update));
timer->start(16);
}
protected:
void paintEvent(QPaintEvent *event) override {
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.translate(width() / 2, height() / 2);
painter.rotate(angle);
painter.drawRect(-50, -50, 100, 100);
angle += 1;
}
private:
int angle = ;
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
Widget widget;
widget.show();
return app.exec();
}
阅读全文