打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
仿qq的错误提示透明
ErrorWidget.h
 
#ifndef ERRORWIDGET_H
#define ERRORWIDGET_H

#include <QPushButton>

#include <QToolButton>

#include  <QWidget>
#include  <QLabel>
class ErrorWidget:public QWidget
{
    Q_OBJECT

public:



   // ErrorWidget();
    ErrorWidget(QWidget *parent);

    ~ErrorWidget();
    void  setTipInfo(QString info);

    void setTipIcon(QPixmap pixmap);
public slots:
    bool closeWidget();

private:
    QToolButton*    close_button;
    QLabel *msg_label;
    QLabel * ask_label;


};
#endif // ERRORWIDGET_H

 
 
 
 
 
ErrorWidget.cpp文件
 
#include <QMessageBox>
#include <QStyle>
#include "ErrorWidget.h"
ErrorWidget::ErrorWidget(QWidget *parent)
 : QWidget(parent)
{
 int width = parent->width();

 this->resize(width, 28);
 //设置标题栏隐藏
 this->setWindowFlags(Qt::FramelessWindowHint);
 //设置背景色透明
 QPalette palette;
 QColor color(190, 230, 250);
 color.setAlphaF(0.6);
 palette.setBrush(this->backgroundRole(), color);
 this->setPalette(palette);
 //如果这个QWidget直接show,是有背景色的,但是如果放到一个父Widget中时,它就没有了效果。添加如下代码后就可以了:
 this->setAutoFillBackground(true);

//构建关闭按钮
 close_button= new QToolButton(this);
 QPixmap close_pix = style()->standardPixmap(QStyle::SP_TitleBarCloseButton);
 close_button->setIcon(close_pix);
 close_button->setStyleSheet("QToolButton{background-color: transparent;}");

 //获取主界面的宽度
 int height = this->height();
 close_button->setGeometry(width-20, 0, 20, 20);
 //设置提示图片
 msg_label = new QLabel(this);
 msg_label->setGeometry(QRect(5, 5, 20, 20));
 msg_label->setStyleSheet("background-color: transparent;");
 msg_label->setScaledContents(true);
 //设置提示信息
 ask_label = new QLabel(this);
 ask_label->setStyleSheet("background-color: transparent; color: red;");
 ask_label->setGeometry(QRect(30, 0, width - 60, height));
 ask_label->setAlignment(Qt::AlignCenter);
 close_button->setCursor(Qt::PointingHandCursor);
 QObject::connect(close_button, SIGNAL(clicked()), this, SLOT(closeWidget()));
}
ErrorWidget::~ErrorWidget()
{
// Setting::freePointer(ask_label);
 //Setting::freePointer(msg_label);
 //Setting::freePointer(close_button);
}
void ErrorWidget::setTipInfo(QString info)
{
 //设置提示信息
 ask_label->setText(info);
}
void ErrorWidget::setTipIcon(QPixmap pixmap)
{
 msg_label->setPixmap(pixmap);
}

//关闭按钮主要进行提示框的隐藏
bool ErrorWidget::closeWidget()
{

  // QMessageBox::warning(this,tr("ss"),tr("333"),QMessageBox::Yes);

 this->hide();
 return true;
}


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
GUI学习之三十四
Qt之信号与槽
关于QT自定义控件
QQRadioButton和QCheckBox用法示例
QT Creator 快速入门教程 读书笔记(二)
Qt 汽车仪表 QWidget
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服