Qt编写自定义控件:在屏幕中心显示信息的控件

这是一个关于自定义QWidget的实现,包括便捷设置背景颜色、边框样式的功能。同时,它包含了一个消息提示框类,能够显示信息并自动隐藏。在Android平台上,需要注意透明背景的设置可能导致显示问题,可以通过修改QSS解决。
摘要由CSDN通过智能技术生成

代码:

#ifndef GENERALCONTROL_WIDGET_H
#define GENERALCONTROL_WIDGET_H

#include <QWidget>

class generalControl_Widget : public QWidget
{
    Q_OBJECT
public:
    explicit generalControl_Widget(int topRadius,int bottomRadius, QString objectName,QWidget *parent = nullptr);
    void gradientThemeStyle(const QString & borderColor,const QString & qlineargradient_startColor,const QString & qlineargradient_endColor);
    void colorThremeStyle(const QString & borderColor, const QString & bgColor,
                          int topBorderWidth = 2, int rightBorderWidth = 2, int bottomBorderWidth = 2, int leftBorderWidth = 2);

protected:
    void paintEvent(QPaintEvent *event)override;

private:
    int topRadius;
    int bottomRadius;
    QString objectName;
};

#endif // GENERALCONTROL_WIDGET_H
#include "generalcontrol_widget.h"
#include <QStyleOption>
#include <QPainter>

generalControl_Widget::generalControl_Widget(int topRadius, int bottomRadius, QString objectName, QWidget *parent)
    : QWidget(parent),topRadius(topRadius),bottomRadius(bottomRadius),objectName(objectName)
{   
    assert(!objectName.isEmpty());
    setObjectName(objectName);
}

void generalControl_Widget::gradientThemeStyle(const QString &borderColor,
                                               const QString &qlineargradient_startColor,
                                               const QString &qlineargradient_endColor)
{
    setStyleSheet(QString("QWidget#%5{border-top-left-radius:%1px;"
                                     "border-top-right-radius:%1px;"
                                     "border-width: 2px;"
                                     "border-style: solid;"
                                     "border-color:#%2;"
                                     "background-color:qlineargradient(x1:0, y1:0 , x2:0 ,y2:1 stop:0 #%3 ,stop:1 #%4);}")
                                      .arg(topRadius)
                                      .arg(borderColor)
                                      .arg(qlineargradient_startColor)
                                      .arg(qlineargradient_endColor)
                                      .arg(objectName));
}

void generalControl_Widget::colorThremeStyle(const QString & borderColor,
                                             const QString & bgColor,
                                             int topBorderWidth,
                                             int rightBorderWidth,
                                             int bottomBorderWidth,
                                             int leftBorderWidth)
{
    setStyleSheet(QString("QWidget#%8{border-top-left-radius:%1px;"
                                    "border-top-right-radius:%1px;"
                                    "border-bottom-left-radius:%9px;"
                                    "border-bottom-right-radius:%9px;"
                                    "border-width: %4px %5px %6px %7px;"
                                    "border-style: solid;"
                                    "border-color:#%2;"
                                    "background-color:#%3;}").arg(topRadius)
                                                          .arg(borderColor)
                                                          .arg(bgColor)
                                                          .arg(topBorderWidth)
                                                          .arg(rightBorderWidth)
                                                          .arg(bottomBorderWidth)
                                                          .arg(leftBorderWidth)
                                                          .arg(objectName).arg(bottomRadius));
}

void generalControl_Widget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QStyleOption styleOpt;
    styleOpt.init(this);
    QPainter painter(this);
    style()->drawPrimitive(QStyle::PE_Widget, &styleOpt, &painter, this);
}

上面是一个自定义的QWidget,可以便捷地设置背景、边框。用作容器。

#ifndef GENERALCONTROL_HOVERSHOWMSG_WIDGET_H
#define GENERALCONTROL_HOVERSHOWMSG_WIDGET_H

#include <QTimer>
#include <QWidget>

class generalControl_hoverShowMSG_Widget : public QWidget/*generalControl_Widget*/
{
    Q_OBJECT
public:
    static generalControl_hoverShowMSG_Widget *getInstance();
    void setInfoText(const QString &text);
    void set_style();

protected:
    QSize sizeHint() const override;

private:
    class QLabel * textLabel;
    explicit generalControl_hoverShowMSG_Widget(QWidget *parent = nullptr);
    static generalControl_hoverShowMSG_Widget * instance;
    class CGarbo
    {
    public:
        ~CGarbo()
        {
            if(generalControl_hoverShowMSG_Widget::instance)
            {
                generalControl_hoverShowMSG_Widget::instance->deleteLater();
            }
        }
    };
    static CGarbo Garbo;
    QTimer timer;
    class QFrame * frame;
};

#endif // GENERALCONTROL_HOVERSHOWMSG_WIDGET_H
#include "generalcontrol_hovershowmsg_widget.h"
#include <QLabel>
#include <QHBoxLayout>
#include <QGraphicsDropShadowEffect>
#include <QDesktopWidget>
#include <QApplication>
#include "common/common.h"

generalControl_hoverShowMSG_Widget *generalControl_hoverShowMSG_Widget::instance = nullptr;
generalControl_hoverShowMSG_Widget *generalControl_hoverShowMSG_Widget::getInstance()
{
    if (!instance)
    {
        instance = new generalControl_hoverShowMSG_Widget();
    }
    return instance;
}

void generalControl_hoverShowMSG_Widget::setInfoText(const QString & text)
{
    textLabel->setText(text);
    timer.start();
    show();
    adjustSize();

    move((QApplication::desktop()->width() - this->width()) / 2,(QApplication::desktop()->height() - this->height()) * 0.25);
}

void generalControl_hoverShowMSG_Widget::set_style()
{
    if(currentThemeIndex < 11)
    {
        setWindowOpacity(0.8);
        frame->setStyleSheet(QString("QFrame#frame{background-color:#%1;border-radius:10px;}").arg(style_main_color));
    }
    else
    {
        setWindowOpacity(1);
        frame->setStyleSheet(QString("QFrame#frame{background-color:#%1;border-radius:10px;}").arg(lineEdit_and_combox_Bg));
    }
}

QSize generalControl_hoverShowMSG_Widget::sizeHint() const
{
    auto fontMetics = this->fontMetrics();
    auto size = fontMetics.size(Qt::AlignCenter,textLabel->text());
    return size;
}

generalControl_hoverShowMSG_Widget::generalControl_hoverShowMSG_Widget(QWidget *parent)
    : QWidget(parent)
{
    setAttribute(Qt::WA_TranslucentBackground);
    setWindowFlag(Qt::FramelessWindowHint);
    setWindowFlag(Qt::WindowStaysOnTopHint);
    setWindowFlag(Qt::ToolTip);

    setWindowOpacity(0.8);

    textLabel = new QLabel(this);
    textLabel->setAlignment(Qt::AlignCenter);
    textLabel->setStyleSheet("color:#FFFFFF;");

    auto hb = new QHBoxLayout;
    hb->addWidget(textLabel);
    frame = new QFrame(this);
    frame->setObjectName("frame");
    frame->setLayout(hb);
    hb->setMargin(10);

    hb = new QHBoxLayout;
    hb->addWidget(frame);

    QGraphicsDropShadowEffect * effect = new QGraphicsDropShadowEffect(this);
    effect->setOffset(0, 0);
    effect->setColor(QColor("#444444"));
    effect->setBlurRadius(10);
    frame->setGraphicsEffect(effect);
    hb->setMargin(10);

    setLayout(hb);

    connect(&timer,&QTimer::timeout,this,&generalControl_hoverShowMSG_Widget::close);
    timer.setSingleShot(true);
    timer.setInterval(3000);

    auto font = this->font();
    font.setPixelSize(30);
    font.setBold(true);
    setFont(font);
}

使用:

generalControl_hoverShowMSG_Widget *generalControl_hoverShowMSG_Widget::getInstance()->setInfoText();

如果在安卓平台,设置 setAttribute(Qt::WA_TranslucentBackground);后如果不设置窗口的parent,那么整个窗口会一片黑色,解决办法是设置qss:

相关博文:QFontMetrics

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值