Qt之透明提示框
2013-09-10 22:49
246 查看
代码来自 http://blog.sina.com.cn/s/blog_a6fb6cc90101az3h.html
![](http://img.blog.csdn.net/20130910224823343)
Qt实现类似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();
}
Qt实现类似QQ的登录失败的提示框,主要涉及窗口透明并添加关闭按钮,以及图标和信息的显示等。
直接上代码:
#ifndef ERRORWIDGET_H #define ERRORWIDGET_H #include <QtGui/QWidget> #include <QtGui> class ErrorWidget : public QWidget { Q_OBJECT public: ErrorWidget(QWidget *parent = 0); ~ErrorWidget(); void setTipInfo(QString info); void setTipIcon(QPixmap pixmap); private: QToolButton *close_button; QLabel *msg_label; QLabel *ask_label; private slots: bool closeWidget(); }; #endif // ERRORWIDGET_H
#include "errorwidget.h" #include <QDebug> 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())); /* QHBoxLayout *hLayout = new QHBoxLayout; hLayout->addWidget(msg_label); hLayout->addWidget(ask_label); hLayout->addWidget(close_button); setLayout(hLayout); */ } 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; }
#include <QtGui/QApplication> #include "dialog.h" #include "errorwidget.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); QPushButton *bt1 = new QPushButton("test"); bt1->setFixedSize(300, 200); ErrorWidget *error = new ErrorWidget(bt1); error->setTipInfo("<font color=green>error</font>"); bt1->show(); return a.exec(); }
提示框透明,且包含提示图标,关闭按钮等!
所以自己也可以利用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();
}
相关文章推荐
- qt之透明提示框
- Qt 设置按钮透明、图标、移动到按钮和状态栏提示等各种属性
- Qt之透明提示框(模拟QQ)
- qt之 透明提示框
- Qt: 制作透明无边框提示窗口.
- Qt之透明提示框
- qt之透明提示框(模拟qq) (非常漂亮)
- Qt 设置透明按钮其实很简单 【开源中国】
- Qt 无边框、透明、可移动的个性窗体
- ubuntu系统启动qtceator时提示:Qt5.5.1/Tools/QtCreator/lib/qtcreator/plugins/libHelp.so: 无法加载库
- 使用Visual Studio(VS)开发Qt程序代码提示功能的实现
- CSS Filter背景透明提示
- QT5提示can not find -lGL的解决方法
- QT中创建透明或者异性的Window
- QT5提示can not find -lGL的解决方法
- Qt程序在开发板上运行,提示不能创建信号量。
- 打开qtcreator出现提示无法覆盖文件 /home/xxx/.config/Qtcreator/qtversion.xml : Permission denied
- QT:关于qt工程在编译时提示undefined reference tp 'XXX::staticMetaObject'
- 运行qtcreator 出现错误提示:error: Qt Creator needs a compiler set up to build. Configure a compile
- QT 智能提示设置