zoukankan      html  css  js  c++  java
  • QT笔记之实现阴影窗口

    方法一:

    代码实现

    在窗口构造函数中加入:setAttribute(Qt::WA_TranslucentBackground),保证不被绘制上的部分透明

    重写void paintEvent(QPaintEvent *event);

    void QT_Test::paintEvent(QPaintEvent *event)
    {
        QPainterPath path;
        path.setFillRule(Qt::WindingFill);
        path.addRect(10, 10, this->width()-20, this->height()-20);
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing, true);
        painter.fillPath(path, QBrush(Qt::white));
    
        QColor color(0, 0, 0, 50);
        for(int i=0; i<10; i++)
        {
            QPainterPath path;
            path.setFillRule(Qt::WindingFill);
            path.addRect(10-i, 10-i, this->width()-(10-i)*2, this->height()-(10-i)*2);
            color.setAlpha(150 - qSqrt(i)*50);
            painter.setPen(color);
            painter.drawPath(path);
        }
    }

    方法二:

    用代用阴影的背景图片实现

    QT的窗口对于一般的窗口程序来说,已经完全够用了。但有时候我们要求界面比较精美,或者还想自定义皮肤之类的话,就需要自己定义窗口。这里介绍一种简单的自定义窗口的方法。

    自定义样式可以达到很多的自定义皮肤的效果,但自定义样式有时不能指定窗口的形状,或者实现窗口的阴影效果(使用QT的QGraphicsEffect定义阴影,但运行效率较低)。这样的话可以重载窗口的paintEvent函数实现自绘制窗口。

    先准备一张有窗口阴影的背景图,然后在paintEvent函数里面使用QPainterx绘制这张图。

    这里将窗口类命名为GraphicDialog

    示例代码如下:

    class GraphicDialog :
    public QDialog
    {
    public:
    GraphicDialog(QWidget* parent = NULL, Qt::WindowFlags f = 0/* Qt::FramelessWindowHint*/);
    ~GraphicDialog(void);
    protected:
    void paintEvent(QPaintEvent *);
    QPixmap background;
    };

    在窗口类构造函数中:

    setWindowFlags(Qt::FramelessWindowHint); //无标题窗口
    setAttribute(Qt::WA_TranslucentBackground);

    background.load(":/Images/DialogBackground");
    在paintEvent中

    QPainter p(this);
    p.drawPixmap(0, 0, rect().width(), rect().height(), background);

    实现效果如图:

    QT实现阴影窗口(一)(转) - twyok - twyok的博客

    转载:http://twyok.blog.163.com/blog/static/812293032013215506418/

  • 相关阅读:
    【以前的空间】poj 2288 Islands and Bridges
    【以前的空间】网络流合集
    【以前的空间】 单调队列系列
    【以前的空间】几道平衡树
    【以前的空间】vijos 1720 阿狸的打字机
    对拍程序
    【Ubuntu】编写一个c语言代码
    用Matlab对数据进行线性拟合算法
    Oracle视图传递参数
    oracle decode的用法
  • 原文地址:https://www.cnblogs.com/chechen/p/5597276.html
Copyright © 2011-2022 走看看