《QT实用小工具·三》偏3D风格的异型窗体

1、概述
源码放在文章末尾

可以在窗体中点击鼠标左键进行图片切换,项目提供了一些图片素材,整体风格偏向于3D类型,也可以根据需求自己放置不同的图片。

下面是demo演示:
在这里插入图片描述
项目部分代码如下所示:
头文件部分:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

protected:
    bool eventFilter(QObject *watched, QEvent *evt);

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H

逻辑实现部分:

#include "widget.h"
#include "ui_widget.h"
#include "qevent.h"
#include "qdebug.h"

Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
{
    ui->setupUi(this);

    this->setAttribute(Qt::WA_TranslucentBackground);
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinimizeButtonHint);
    ui->widget->installEventFilter(this);
    ui->widget->setStyleSheet(QString("background-image:url(:/image/%1.png);").arg(1));
}

Widget::~Widget()
{
    delete ui;
}

bool Widget::eventFilter(QObject *watched, QEvent *evt)
{
    static int index = 1;
    static QPoint mousePoint;
    static bool mousePressed = false;

    QMouseEvent *event = static_cast<QMouseEvent *>(evt);
    if (event->type() == QEvent::MouseButtonPress) {
        if (event->button() == Qt::LeftButton) {
            mousePressed = true;
            mousePoint = event->globalPos() - this->pos();

            if (index == 5) {
                index = 1;
            } else {
                index++;
            }

            ui->widget->setStyleSheet(QString("background-image:url(:/image/%1.png);").arg(index));

            return true;
        } else {
            exit(0);
        }
    } else if (event->type() == QEvent::MouseButtonRelease) {
        mousePressed = false;
        return true;
    } else if (event->type() == QEvent::MouseMove) {
        if (mousePressed && (event->buttons() && Qt::LeftButton)) {
            this->move(event->globalPos() - mousePoint);
            return true;
        }
    }

    return QWidget::eventFilter(watched, event);
}

源码下载

最近更新

  1. TCP协议是安全的吗?

    2024-03-29 14:48:02       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-29 14:48:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-29 14:48:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-29 14:48:02       20 阅读

热门阅读

  1. websocket

    websocket

    2024-03-29 14:48:02      15 阅读
  2. LeetCode-热题100:238. 除自身以外数组的乘积

    2024-03-29 14:48:02       13 阅读
  3. .NET core 5.0 及以上的Windows Service开发

    2024-03-29 14:48:02       20 阅读
  4. SpringBoot -- 自动配置机制

    2024-03-29 14:48:02       21 阅读
  5. 牛客的一道题(C)变种水仙花

    2024-03-29 14:48:02       15 阅读
  6. Qt使用事件过滤器

    2024-03-29 14:48:02       15 阅读
  7. Pytorch nn.Linear()

    2024-03-29 14:48:02       18 阅读
  8. mybatis plus 数据权限插件在项目中的使用

    2024-03-29 14:48:02       17 阅读
  9. C语言——函数练习程序

    2024-03-29 14:48:02       20 阅读
  10. 解决跨域问题

    2024-03-29 14:48:02       22 阅读
  11. svg怎么用,后端返回svg文件流引入

    2024-03-29 14:48:02       17 阅读