您的位置:首页 > 汽车 > 新车 > 外语人才网官网_中国外贸平台排名_今日全国疫情最新消息_游戏推广员是违法的吗

外语人才网官网_中国外贸平台排名_今日全国疫情最新消息_游戏推广员是违法的吗

2025/5/2 9:51:47 来源:https://blog.csdn.net/A185822153/article/details/146072402  浏览:    关键词:外语人才网官网_中国外贸平台排名_今日全国疫情最新消息_游戏推广员是违法的吗
外语人才网官网_中国外贸平台排名_今日全国疫情最新消息_游戏推广员是违法的吗

要在Qt中实现一个带有转动风扇效果,可以按照以下步骤进行:

1. 创建一个自定义按钮类

继承QPushButton,并在其中添加绘制风扇动画的功能。

#include <QtWidgets/QPushButton>
#include <QtWidgets/QLabel>
#include <QtCore/QTimer>
#include <QtGui/QPainter>
#include <QtGui/QPaintEvent>class AnimatedFanButton : public QPushButton {
public:AnimatedFanButton(const QString &text, QWidget *parent = nullptr): QPushButton(text, parent) {// 初始化风扇旋转角度rotationAngle = 0;// 创建一个定时器,用于更新旋转角度QTimer *timer = new QTimer(this);connect(timer, &QTimer::timeout, this, &AnimatedFanButton::updateRotation);timer->start(50); // 每50毫秒更新一次}protected:void paintEvent(QPaintEvent *event) override {QPushButton::paintEvent(event);// 在按钮上绘制风扇QPainter painter(this);painter.setRenderHint(QPainter::Antialiasing); // 启用抗锯齿// 设置绘制中心点int radius = qMin(width(), height()) / 3;int centerX = width() / 2;int centerY = height() / 2;// 绘制风扇叶片QPen pen(Qt::black, 3);painter.setPen(pen);painter.translate(centerX, centerY); // 平移到中心点painter.rotate(rotationAngle); // 应用旋转角度// 绘制三个叶片for (int i = 0; i < 3; ++i) {painter.drawLine(0, -radius, 0, radius/2);painter.rotate(120); // 每次旋转120度}painter.end();}private slots:void updateRotation() {rotationAngle += 3; // 每次旋转3度if (rotationAngle >= 360) {rotationAngle -= 360;}update(); // 刷新界面}private:int rotationAngle;
};

2 创建主窗口并添加按钮

在主窗口中创建并显示带有转动风扇的按钮。

#include <QtWidgets/QApplication>
#include <QtWidgets/QWidget>int main(int argc, char *argv[]) {QApplication app(argc, argv);QWidget window;window.resize(400, 400);window.setStyleSheet("background-color: #f0f0f0;");AnimatedFanButton button("Click Me!", &window);button.setGeometry(100, 100, 100, 100);button.show();return app.exec();
}

3 另外如果你想实现拖动功能

在自定义按钮类中添加鼠标事件处理,使按钮可以被拖动。

class AnimatedFanButton : public QPushButton {// ... 其他代码保持不变 ...protected:void mousePressEvent(QMouseEvent *event) override {QPushButton::mousePressEvent(event);if (event->button() == Qt::LeftButton) {dragStartPos = event->globalPos();buttonPos = pos();}}void mouseMoveEvent(QMouseEvent *event) override {if (event->buttons() & Qt::LeftButton) {QPoint delta = event->globalPos() - dragStartPos;move(buttonPos + delta);event->accept();}}void mouseReleaseEvent(QMouseEvent *event) override {QPushButton::mouseReleaseEvent(event);// 拖动结束时重置变量dragStartPos = QPoint();buttonPos = QPoint();}private:QPoint dragStartPos;QPoint buttonPos;
};

4. 运行程序

编译并运行程序,你将看到一个带有旋转风扇的悬浮按钮,可以点击并拖动到任意位置。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com