当前位置: 首页> 娱乐> 明星 > 天津建网站_江苏seo推广方案_营销的主要目的有哪些_百度搜索推广优化师工作内容

天津建网站_江苏seo推广方案_营销的主要目的有哪些_百度搜索推广优化师工作内容

时间:2025/7/16 15:32:45来源:https://blog.csdn.net/A185822153/article/details/146073803 浏览次数:0次
天津建网站_江苏seo推广方案_营销的主要目的有哪些_百度搜索推广优化师工作内容

        要在 Qt 中实现抽屉效果(类似于侧边栏滑出),可以使用 QWidget 和 QPropertyAnimation 来创建滑动动画效果。以下是实现步骤:

1. 创建抽屉类

创建一个自定义的抽屉类,继承自 QWidget,并在其中实现滑动动画。

#include <QtWidgets/QWidget>
#include <QtWidgets/QPushButton>
#include <QtCore/QPropertyAnimation>
#include <QtCore/QString>class Drawer : public QWidget {
public:Drawer(QWidget *parent = nullptr): QWidget(parent) {// 初始化抽屉状态isExpanded = false;// 设置抽屉的大小setFixedWidth(250); // 抽屉宽度setMinimumHeight(parent->height()); // 高度与父窗口一致// 设置抽屉的初始位置(隐藏在屏幕左侧)move(-width(), 0);}void toggle() {if (isExpanded) {// 收回抽屉QPropertyAnimation *animation = new QPropertyAnimation(this, "pos");animation->setDuration(300);animation->setStartValue(QPoint(0, 0));animation->setEndValue(QPoint(-width(), 0));animation->setEasingCurve(QEasingCurve::OutQuad);animation->start();isExpanded = false;} else {// 展开抽屉QPropertyAnimation *animation = new QPropertyAnimation(this, "pos");animation->setDuration(300);animation->setStartValue(QPoint(-width(), 0));animation->setEndValue(QPoint(0, 0));animation->setEasingCurve(QEasingCurve::OutQuad);animation->start();isExpanded = true;}}protected:void paintEvent(QPaintEvent *event) override {// 自定义抽屉的样式QPainter painter(this);painter.fillRect(rect(), QColor("#333")); // 设置背景颜色painter.setPen(Qt::white);painter.drawText(rect(), Qt::AlignCenter, "抽屉内容");}private:bool isExpanded;
};

2. 创建主窗口并添加抽屉

在主窗口中创建抽屉和一个按钮,用于触发抽屉的展开和收回。

#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QPushButton>int main(int argc, char *argv[]) {QApplication app(argc, argv);QMainWindow window;window.setWindowTitle("抽屉效果示例");window.resize(800, 600);// 创建抽屉Drawer *drawer = new Drawer(&window);drawer->show();// 创建一个按钮,用于触发抽屉的展开和收回QPushButton *button = new QPushButton("打开抽屉", &window);button->setGeometry(20, 20, 100, 30);button->setStyleSheet("background-color: #4CAF50; color: white;");// 连接按钮点击事件到抽屉的 toggle 方法QObject::connect(button, &QPushButton::clicked, drawer, &Drawer::toggle);window.show();return app.exec();
}

4 效果如下:

5. 功能说明

  • 抽屉类 (Drawer)

    • 初始化时,抽屉位于屏幕左侧(隐藏状态)。
    • toggle 方法用于展开或收回抽屉,使用 QPropertyAnimation 实现平滑的滑动效果。
    • paintEvent 方法用于自定义抽屉的样式。
  • 主窗口

    • 创建一个按钮,点击后调用抽屉的 toggle 方法。
    • 按钮的样式可以通过 setStyleSheet 自定义。

6. 自定义样式和动画

你可以根据需要调整以下参数:

  • 抽屉样式

    • 修改 Drawer::paintEvent 中的样式,例如背景颜色、文字内容等。
    • 在 Drawer 构造函数中添加更多的 UI 组件,例如按钮、标签等。
  • 动画效果

    • 修改 QPropertyAnimation 的 duration 属性调整动画速度。
    • 修改 setEasingCurve 的参数调整动画的缓动效果(例如 QEasingCurve::OutCubic 或 QEasingCurve::InOutQuart)。
关键字:天津建网站_江苏seo推广方案_营销的主要目的有哪些_百度搜索推广优化师工作内容

版权声明:

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

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

责任编辑: