Qt编写自定义控件:不规则弹窗小部件

代码:

#ifndef IRREGULARPOPUPWIDGET_H
#define IRREGULARPOPUPWIDGET_H

#include <QWidget>

class IrregularPopupWidget : public QWidget
{
    Q_OBJECT

public:
    IrregularPopupWidget(QWidget *parent = nullptr);
    ~IrregularPopupWidget()override;

    void setText(const QString &newText);
    enum class SharpCornersPosition
    {
        left = 0,
        right,
        top,
        bottom
    };
    void setWidgetSharpCornersPosition(SharpCornersPosition newWidgetSharpCornersPosition);

protected:
    void paintEvent(QPaintEvent *event)override;
    QSize sizeHint() const override;

private:
    SharpCornersPosition widgetSharpCornersPosition{SharpCornersPosition::left};
    int SharpCornersRectWidth{30};
    int SharpCornersRectHeight{30};
    QColor backgroundColor{"#31305a"};
    QString text;
};
#endif // IRREGULARPOPUPWIDGET_H
#include "irregularpopupwidget.h"
#include <QPainter>
#include <QPainterPath>
#include <QPaintEvent>
#include <QGraphicsDropShadowEffect>

IrregularPopupWidget::IrregularPopupWidget(QWidget * parent)
    :QWidget(parent)
{
    setPalette(Qt::white);

    QGraphicsDropShadowEffect * effect = new QGraphicsDropShadowEffect(this);
    effect->setOffset(0,0);
    effect->setBlurRadius(25);
    effect->setColor(Qt::black);
    setGraphicsEffect(effect);

    auto font = this->font();
    font.setPixelSize(20);
    setFont(font);

    this->setWindowFlags(Qt::FramelessWindowHint);
    this->setAttribute(Qt::WA_TranslucentBackground, true);
}

IrregularPopupWidget::~IrregularPopupWidget()
{
}

QSize IrregularPopupWidget::sizeHint() const
{
    if(text.isEmpty())
    {
        return QSize(200, 100);
    }
    else
    {
        auto fontMetics = this->fontMetrics();
        auto size = fontMetics.size(Qt::AlignCenter,text);
        int width = size.width();
        int height = size.height();
        switch (widgetSharpCornersPosition)
        {
            case SharpCornersPosition::left:
            case SharpCornersPosition::right:
            {
                width += SharpCornersRectWidth;
            }break;
            case SharpCornersPosition::top:
            case SharpCornersPosition::bottom:
            {
                height += SharpCornersRectHeight;
            }break;
        }
        width += 30;
        height += 30;
        return QSize(width,height);
    }
}

void IrregularPopupWidget::setWidgetSharpCornersPosition(SharpCornersPosition newWidgetSharpCornersPosition)
{
    widgetSharpCornersPosition = newWidgetSharpCornersPosition;
}

void IrregularPopupWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing,true);
    const auto rect = event->rect();
    auto width = rect.width();
    auto height = rect.height();
    painter.fillRect(rect,Qt::transparent);

    QPainterPath path;
    QPolygonF polygon;
    QRect centerRect;
    switch (widgetSharpCornersPosition)
    {
        case SharpCornersPosition::left:
        {
            polygon.append(QPointF(SharpCornersRectWidth,(height - SharpCornersRectHeight) / 2));
            polygon.append(QPointF(0,(height) / 2));
            polygon.append(QPointF(SharpCornersRectWidth,(height) / 2 + SharpCornersRectHeight/2));
            centerRect = QRect(SharpCornersRectWidth-5,0,(width - SharpCornersRectWidth),height);
        }break;
        case SharpCornersPosition::right:
        {
            polygon.append(QPointF(width - SharpCornersRectWidth,(height - SharpCornersRectHeight) / 2));
            polygon.append(QPointF(width,(height) / 2));
            polygon.append(QPointF(width - SharpCornersRectWidth,(height) / 2 + SharpCornersRectHeight/2));
            centerRect = QRect(0,0,(width - SharpCornersRectWidth),height);
        }break;
        case SharpCornersPosition::top:
        {
            polygon.append(QPointF((width - SharpCornersRectWidth) / 2,SharpCornersRectHeight));
            polygon.append(QPointF((width) / 2,0));
            polygon.append(QPointF(width / 2 + SharpCornersRectWidth/2,SharpCornersRectHeight));
            centerRect = QRect(0,SharpCornersRectHeight,width,height - SharpCornersRectHeight);
        }break;
        case SharpCornersPosition::bottom:
        {
            polygon.append(QPointF((width - SharpCornersRectWidth) / 2,height - SharpCornersRectHeight));
            polygon.append(QPointF((width) / 2,height));
            polygon.append(QPointF(width / 2 + SharpCornersRectWidth/2,height - SharpCornersRectHeight));
            centerRect = QRect(0,0,width,height - SharpCornersRectHeight);
        }
    }

    painter.setBrush(backgroundColor);
    painter.setPen(Qt::transparent);
    painter.drawPolygon(polygon);
    painter.drawRoundedRect(centerRect,6,6);
    path.addPolygon(polygon);
    path.addRoundedRect(centerRect,6,6);
    painter.setClipPath(path);

    painter.setPen(Qt::white);
    painter.drawText(centerRect,Qt::AlignCenter | Qt::TextWordWrap,text);
}

void IrregularPopupWidget::setText(const QString &newText)
{
    text = newText;
}

使用:

    IrregularPopupWidget w;
    w.setWidgetSharpCornersPosition(IrregularPopupWidget::SharpCornersPosition::top);
    w.setText("黄河之水天上来");
    w.show();

    IrregularPopupWidget w;
    w.setWidgetSharpCornersPosition(IrregularPopupWidget::SharpCornersPosition::left);
    w.setText("黄河之水天上来");
    w.show();

 

    IrregularPopupWidget w;
    w.setWidgetSharpCornersPosition(IrregularPopupWidget::SharpCornersPosition::right);
    w.setText("黄河之水天上来");
    w.show();

 

    IrregularPopupWidget w;
    w.setWidgetSharpCornersPosition(IrregularPopupWidget::SharpCornersPosition::bottom);
    w.setText("黄河之水天上来");
    w.show();

 

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值