qt之透明提示框(模擬qq) (非常漂亮)


   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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM