Qt之透明提示框(模拟QQ)
2014-10-23 14:16
176 查看
Qt实现类似QQ的登录失败的提示框,主要涉及窗口透明并添加关闭按钮,以及图标和信息的显示等。
直接上代码:
#include "error_widget.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()
{
this->hide();
return true;
}
实现思路:
QQ效果图:
提示框透明,且包含提示图标,关闭按钮等!
所以自己也可以利用Qt中的QWidget创建一个提示框,在构建的时候设置背景透明,但是进行窗口隐藏,使用hide()(之所以隐藏是因为登录的时候不显示,只有在登录失败的时候才显示,即调用show()),再登录失败之后调用setTipIcon(QPixmap pixmap)设置图标和setTipInfo(QString info)设置提示信息即可。
//进行错误提示
QPixmap pixmap = QPixmap(":/icon/errortip");
error_widget->setTipIcon(pixmap);
error_widget->setTipInfo(info);
if(error_widget->isHidden())
{
error_widget->show();
}
效果图:
字体颜色样式什么的都可以自行设置,主要是实现的思路!愿大家共勉。
转载自:http://blog.sina.com.cn/s/blog_a6fb6cc90101az3h.html
直接上代码:
#include "error_widget.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()
{
this->hide();
return true;
}
实现思路:
QQ效果图:
提示框透明,且包含提示图标,关闭按钮等!
所以自己也可以利用Qt中的QWidget创建一个提示框,在构建的时候设置背景透明,但是进行窗口隐藏,使用hide()(之所以隐藏是因为登录的时候不显示,只有在登录失败的时候才显示,即调用show()),再登录失败之后调用setTipIcon(QPixmap pixmap)设置图标和setTipInfo(QString info)设置提示信息即可。
//进行错误提示
QPixmap pixmap = QPixmap(":/icon/errortip");
error_widget->setTipIcon(pixmap);
error_widget->setTipInfo(info);
if(error_widget->isHidden())
{
error_widget->show();
}
效果图:
字体颜色样式什么的都可以自行设置,主要是实现的思路!愿大家共勉。
转载自:http://blog.sina.com.cn/s/blog_a6fb6cc90101az3h.html
相关文章推荐
- qt之透明提示框(模拟qq) (非常漂亮)
- 浏览器上模拟qq的消息提示声/网页播放声音
- Qt: 制作透明无边框提示窗口.
- qt类似于qq托盘图标闪烁的消息提示
- Qt 设置按钮透明、图标、移动到按钮和状态栏提示等各种属性
- Qt 之 自定义提示信息框—QQ风格
- qt类似于qq托盘图标闪烁的消息提示
- 模拟MSN和QQ的上线提示效果 区别IE和FF浏览器
- Qt4.8.2 模仿QQ右下角弹窗提示
- Qt之透明提示框
- qt之 透明提示框
- Qt之透明提示框
- qt之透明提示框
- 用setTimeout模拟QQ延时提示框
- qt类似于qq托盘图标闪烁的消息提示 (胡峰原创,代码已经过测试运行)
- C#模拟QQ截屏功能
- vs2010 qtadmin配置qt路径时提示错误“This Qt version uses an unsupported makefile generator ”
- QT实现窗口透明的方法
- iOS QQ第三方登录 提示所安装的QQ不是最新版本的问题
- Qt 之 模仿 QQ登陆界面——样式篇