简述

经常使用企鹅的小伙伴一定对登录失败的提示框很熟悉,主要涉及窗口透明并添加图标、提示信息、关闭按钮的显示等。

我们可以利用QWidget创建一个提示框,然后通过样式设置我们想要的效果。

效果

源码

QMessageWidget.h

#ifndef MESSAGE_WIDGET
#define MESSAGE_WIDGET #include <QWidget> class QLabel;
class QPushButton; class QMessageWidget : public QWidget
{
Q_OBJECT public:
explicit QMessageWidget(QWidget *parent = 0);
~QMessageWidget();
// 设置显示文本
void setText(const QString &text); protected:
void paintEvent(QPaintEvent *event); private:
QLabel *m_pMessageLabel;
}; #endif // MESSAGE_WIDGET

QMessageWidget.cpp

#include <QLabel>
#include <QStyleOption>
#include <QPainter>
#include <QPushButton>
#include <QHBoxLayout>
#include "QMessageWidget.h" QMessageWidget::QMessageWidget(QWidget *parent)
: QWidget(parent)
{
setFixedHeight(25); setAutoFillBackground(true);
setObjectName("messageWidget"); // 提示图标
QLabel *pIconLabel = new QLabel(this);
m_pMessageLabel = new QLabel(this);
QPushButton *pCloseButton = new QPushButton(this); pCloseButton->setFixedSize(8, 8);
pIconLabel->setFixedSize(16, 16); pIconLabel->setScaledContents(true);
pIconLabel->setObjectName("informationLabel"); m_pMessageLabel->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
m_pMessageLabel->setObjectName("highlightLabel");
m_pMessageLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); pCloseButton->setObjectName("closeTipButton"); QHBoxLayout *pLayout = new QHBoxLayout();
pLayout->addWidget(pIconLabel);
pLayout->addWidget(m_pMessageLabel);
pLayout->addWidget(pCloseButton);
pLayout->setSpacing(5);
pLayout->setContentsMargins(3, 3, 5, 3); setLayout(pLayout); connect(pCloseButton, SIGNAL(clicked(bool)), this, SLOT(close()));
} QMessageWidget::~QMessageWidget()
{ } // 设置显示文本
void QMessageWidget::setText(const QString &text)
{
m_pMessageLabel->setText(text);
} // 设置样式需要重写
void QMessageWidget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event); QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

样式

// 界面样式
QWidget#messageWidget {
background: rgba(255, 255, 255, 20%);
} // 提示信息样式
QLabel#highlightLabel {
color: rgb(0, 160, 230);
} // 图标样式
QPushButton#closeTipButton {
border-radius: none;
border-image: url(:/Images/tipClose);
background: transparent;
}
QPushButton#closeTipButton:hover {
border-image: url(:/Images/tipCloseHover);
}
QPushButton#closeTipButton:pressed {
border-image: url(:/Images/tipClosePressed);
}

这里实现了设置信息,贾娜比隐藏等效果,可以在此基础上进行扩展。背景色、字体颜色、图标等样式都可以自行设置,主要是实现思路,愿大家共勉。

Qt之透明提示框的更多相关文章

  1. qt之透明提示框(模拟qq) (非常漂亮)

    Qt实现类似QQ的登录失败的提示框,主要涉及窗口透明并添加关闭按钮,以及图标和信息的显示等. 直接上代码: #include "error_widget.h" ErrorWidge ...

  2. Qt之等待提示框(QMovie)

    简述 关于gif的使用在实际项目中我用的并不多,因为我感觉瑕疵挺多的,很多时候锯齿比较严重,当然与图存在很大的关系. 关于生成gif的方法可以提供一个网站preloaders,基本是可以满足需求的. ...

  3. Qt之等待提示框(QTimer)

    简述 上节讲述了关于QPropertyAnimation实现等待提示框的显示,本节我们使用另外一种方案来实现-使用定时器QTimer,通过设置超时时间定时更新图标达到旋转效果. 简述 效果 资源 源码 ...

  4. Qt之等待提示框(QPropertyAnimation)

    简述 之前分享过QLabel可以通过QMovie播放gif图片,可以实现等待提示框,今天主要使用动画QPropertyAnimation来进行实现! 数据加载的时候,往往都需要后台线程进行数据请求,而 ...

  5. Qt实现冒泡提示框

    通过QLabel创建类似冒泡方式的提示框(提示框显示位置为父类控件居中位置,具体可根据需要自行修改),鼠标停留提示框界面时查看信息,离开时自动淡化消失的效果: 头文件定义 #ifndef _TTipW ...

  6. Qt之等待提示框三(QLabel进行多图片切换)

    之前分享过的等待提示框有用QMovie播放gif图片实现的,也有纯代码实现的,今天再次分享另一种实现方式,如题目所示:QLabel进行图片的切换!     进行用户登录的时候,往往都需要后台线程进行用 ...

  7. Qt之QProgressIndicator(等待提示框)

    简述 很早以前在网上看到一个纯代码实现的旋转动画感觉效果很不错,分享给大家.不得不说,条条大道通罗马,我们需要更多地创造... 详见:QProgressIndicator 简述 效果 源码 使用 更多 ...

  8. Qt 信息提示框 QMessageBox

    information QMessageBox::information(NULL, "Title","Content",QMessageBox::Yes | ...

  9. QPainterPath 不规则提示框

    currentPosition()是最后一次绘制后的“结束点”(或初始点),使用moveTo()移动currentPosition()而不会添加任何元素. QPainterPath ​合并: 1.方法 ...

随机推荐

  1. Hadoop伪分布模式配置

    本作品由Man_华创作,采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可.基于http://www.cnblogs.com/manhua/上的作品创作. 请先按照上一篇文章H ...

  2. Java正则表达式匹配例子

    Java正则表达式匹配例子 package com.ibm.test; import java.util.regex.Matcher; import java.util.regex.Pattern; ...

  3. Sqli-labs less 19

    Less-19 从源代码中我们可以看到我们获取到的是HTTP_REFERER 那和less18是基本一致的,我们从referer进行修改. 还是像less18一样,我们只给出一个示例 将referer ...

  4. 全7 天玩转 ASP.NET MVC — 第 2 天

    0. 前言 我相信在开始第 2 天的学习时,你已经顺利地完成了第 1 天的课程. 我们回顾一下第 1 天的主要关注点: 为什么选择 ASP.NET MVC ? ASP.NET Webforms 和 A ...

  5. 动态调用webservice 接口

    1.url:http://localhost:8002/名称.asmx(asmx结尾) 2.需要引用的命名空间:System.Web.Services 3.调用代码: public class Dyn ...

  6. 6 tips for recovering from a flop

    6 tips for recovering from a flop职场没有失败:6招走出工作失误阴影"We all make mistakes, if we're going to lear ...

  7. jsp中利用checkbox进行批量删除

    一.将前台jsp页面中的所有你要用到checkbox的name值设为相同,如 <input type="checkbox" name="userid"&g ...

  8. Grub命令行

    今天电脑无缘无故无法正常启动,只提示 GRUB> 看来是GRUB引导出问题了,要解决下. 先 想到用制作U盘启动盘来启动,参照网上的方法,很简单用USBBOOT软件做了一个U盘启动盘,按F11在 ...

  9. android+apimonitor+genymotion

    1. 安装genymotion: http://www.genymotion.net/ 2. 设置使用adb Setting--adb--选择sdk的目录 3. apimonitor https:// ...

  10. Win8.1安装VirtualSVN Server发生service visualSVN Server failed to start解决办法

    Service 'VisualSVN Server' failed to start. Please check VisualSVN Server log in Event Viewer for mo ...