Qt自定义消息提示框

1、边框阴影

QGraphicsDropShadowEffect* shadow = new QGraphicsDropShadowEffect(this);
//设置阴影距离
shadow->setOffset(0, 0);
//设置阴影颜色
shadow->setColor(QColor("#444444"));
//设置阴影圆角
shadow->setBlurRadius(10);
//给嵌套QWidget设置阴影
ui->textEdit->setGraphicsEffect(shadow);

2、背景透明,无边框

setWindowFlags(Qt::FramelessWindowHint | Qt::Popup );
setWindowOpacity(1);
setAttribute(Qt::WA_TranslucentBackground, true);

3、在UI添加QTextEdit控件,样式如下

QTextEdit
{
border-radius:10px;border: 2px groove gray;
background-color: rgb(187, 206, 230);
}

4、去除Qt::popup自带阴影

#ifdef Q_OS_WIN32
    static bool class_amended = false;
    if (event->type() == QEvent::WinIdChange)
    {
        HWND hwnd = reinterpret_cast<HWND>(winId());
        if (class_amended == false)
        {
            class_amended = true;
            DWORD class_style = ::GetClassLong(hwnd, GCL_STYLE);
            class_style &= ~CS_DROPSHADOW;
            ::SetClassLong(hwnd, GCL_STYLE, class_style); // windows系统函数
        }
    }
#else
    //非模态下才能生效
    if (event->type() == QEvent::ActivationChange)
    {
        if(QApplication::activeWindow() != this)
        {
            this->close();
        }
    }
#endif
    return QDialog::event(event);

5、消息框自适应内容

ui->textEdit->setText(text);
QTextDocument *doc = ui->textEdit->document();
doc->adjustSize();  //这一步不能少

int width = doc->size().toSize().width()+30;
int height = doc->size().toSize().height()+10;

width = width<120 ? 120 : width;

height = height<75 ? 75 : height;

this->setFixedSize(width ,height);

后感:以上参看网上,整理的,感谢分享!!!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值