Qt之透明提示框
简述
经常使用企鹅的小伙伴一定对登录失败的提示框很熟悉,主要涉及窗口透明并添加图标、提示信息、关闭按钮的显示等。
我们可以利用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之透明提示框的更多相关文章
- qt之透明提示框(模拟qq) (非常漂亮)
Qt实现类似QQ的登录失败的提示框,主要涉及窗口透明并添加关闭按钮,以及图标和信息的显示等. 直接上代码: #include "error_widget.h" ErrorWidge ...
- Qt之等待提示框(QMovie)
简述 关于gif的使用在实际项目中我用的并不多,因为我感觉瑕疵挺多的,很多时候锯齿比较严重,当然与图存在很大的关系. 关于生成gif的方法可以提供一个网站preloaders,基本是可以满足需求的. ...
- Qt之等待提示框(QTimer)
简述 上节讲述了关于QPropertyAnimation实现等待提示框的显示,本节我们使用另外一种方案来实现-使用定时器QTimer,通过设置超时时间定时更新图标达到旋转效果. 简述 效果 资源 源码 ...
- Qt之等待提示框(QPropertyAnimation)
简述 之前分享过QLabel可以通过QMovie播放gif图片,可以实现等待提示框,今天主要使用动画QPropertyAnimation来进行实现! 数据加载的时候,往往都需要后台线程进行数据请求,而 ...
- Qt实现冒泡提示框
通过QLabel创建类似冒泡方式的提示框(提示框显示位置为父类控件居中位置,具体可根据需要自行修改),鼠标停留提示框界面时查看信息,离开时自动淡化消失的效果: 头文件定义 #ifndef _TTipW ...
- Qt之等待提示框三(QLabel进行多图片切换)
之前分享过的等待提示框有用QMovie播放gif图片实现的,也有纯代码实现的,今天再次分享另一种实现方式,如题目所示:QLabel进行图片的切换! 进行用户登录的时候,往往都需要后台线程进行用 ...
- Qt之QProgressIndicator(等待提示框)
简述 很早以前在网上看到一个纯代码实现的旋转动画感觉效果很不错,分享给大家.不得不说,条条大道通罗马,我们需要更多地创造... 详见:QProgressIndicator 简述 效果 源码 使用 更多 ...
- Qt 信息提示框 QMessageBox
information QMessageBox::information(NULL, "Title","Content",QMessageBox::Yes | ...
- QPainterPath 不规则提示框
currentPosition()是最后一次绘制后的“结束点”(或初始点),使用moveTo()移动currentPosition()而不会添加任何元素. QPainterPath 合并: 1.方法 ...
随机推荐
- TestDriven.Net
转: http://www.cnblogs.com/AlexLiu/archive/2008/12/01/1345002.html
- HTTP常见返回代码(HTTP Status codes)的分类和含义
HTTP错误主要分成三类:用户设备问题.Web服务器问题和连接问题.当客户端向Web服务器发送一个HTTP请求时,服务器都会返回一个响应代码.而这些响应代码主要分成五类. HTTP状态码中定义了5大类 ...
- ubuntu安装 scala
1. 配置路径 sudo gedit /etc/profile 2.在文件后面加入 export PATH=/home/sendi/scala-/bin:$PATH 3.更新 source /etc/ ...
- SQL技术内幕-5 比较特殊 insert into 数据的写法
---比较特殊,第一次看到这种写法,记录下来 create table Student --学生成绩表 ( id int, --主键 Grade int, --班级 Score int --分数 ) ...
- POJ 2007 Scrambled Polygon (简单极角排序)
题目链接 题意 : 对输入的点极角排序 思路 : 极角排序方法 #include <iostream> #include <cmath> #include <stdio. ...
- WCF分布式开发步步为赢(7):WCF数据契约与序列化
本节继续学习WCF分布式开发步步为赢(7):WCF数据契约与序列化.数据契约是WCF应用程序开发中一个重要的概念,毫无疑问实现客户端与服务端数据契约的传递中序列化是非常重要的步骤.那么序列化是什么?为 ...
- 最大 / 小的K个数
在<剑指offer>上看到的,而且Qunar去年的校招笔试也考了这题,今天晚上去西电腾讯的宣讲会,来宣讲的学长也说他当时一面的时候面试官问了“一亿个数据的最大的十个数”的面试题.今晚就写写 ...
- 关于playmaker play animation出现警告 The AnimationClip 'xxx' used by the Animati ...
转载自网络: 出现这个提示: The AnimationClip 'xxx' used by the Animation component 'xxx' must be marked as Legac ...
- JAVA实现Excel导出数据(以写好的Excel模版导出)
工作中经常会有将后台数据以Excel导出的功能. 简单的方法有将response的contentType设置为application/vnd.ms-excel: 或在JSP页面直接设置成: <% ...
- ios开发--集成银联3.3.0
项目最近需要集成银联,在网上搜了一下发现都并不是最新版的银联集成教程,自己摸索了一下,总结写了下来. 附上3.3.0的下载网址 https://open.unionpay.com/upload/dow ...