1. 程式人生 > >Qt 實現桌面右下角訊息彈窗提示

Qt 實現桌面右下角訊息彈窗提示

      簡單的做了一個類似QQ訊息提示的訊息彈窗提示的小模組,便於在系統後臺程式提示檢測的資訊,使用Qt開發,設計整體思路是做一個無框架的widget,自己實現標題欄和內容欄,添加了向上移出,自動消隱退出效果,窗體簡單,模組結構便於以後進行擴充套件和移植,旨在顯示文字資訊,通過按鈕操作與主程式進行通訊,執行結果如圖

一、彈窗主體部分 class widget

#include "widget.h"

#include "titlewidget.h"

#include <QVBoxLayout>

#include "mypushbutton.h"

#include <QLabel>

#include <QPainter>

#include <QBitmap>

#include <QDesktopWidget>

#include <QApplication>

#include <QTimer>

#include <QDesktopServices>

 

Widget::Widget(QWidget *parent) :

    QWidget(parent)

{

    setWindowFlags(Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);

isEnter = false;

    titleW=new titleWidget;

    connect(titleW,SIGNAL(myClose()),this,SLOT(close()));

 

    content=new QLabel;

    content->setWordWrap(true);

    content->setAlignment(Qt::AlignTop);

    content->setFixedSize(300,100);

    btnLook=new myPushButton("look.png","檢視");

    connect(btnLook,SIGNAL(clicked()),this,SLOT(seeInfo()));

 

    QVBoxLayout*mainLayout=new QVBoxLayout;

    mainLayout->setMargin(0);

    mainLayout->addWidget(titleW);

    mainLayout->addWidget(content);

    content->setMargin(5);

    mainLayout->addWidget(btnLook,0,Qt::AlignRight);

    setLayout(mainLayout);

 

    setFixedSize(sizeHint().width(),sizeHint().height());

 

    timerShow=new QTimer(this);

    connect(timerShow,SIGNAL(timeout()),this,SLOT(myMove()));

    timerStay=new QTimer(this);

    connect(timerStay,SIGNAL(timeout()),this,SLOT(myStay()));

    timerClose=new QTimer(this);

    connect(timerClose,SIGNAL(timeout()),this,SLOT(myClose()));

}

 

Widget::~Widget()

{

}

 

void Widget::setMsg(QString title, QString content, QString work)

{

    titleW->setTitleText("  "+title);

    this->content->setText("   "+content);

}

 

void Widget::paintEvent(QPaintEvent *)

{

    QBitmap bitmap(this->size());

    bitmap.fill(Qt::white);

    QPainter painter(this);

    painter.setBrush(QBrush(QColor(250,240,230)));

    painter.setPen(QPen(QBrush(QColor(255,222,173)),4));

    painter.drawRoundedRect(bitmap.rect(),5,5);

    setMask(bitmap);

}

void Widget::showAsQQ()

{

    QDesktopWidget *deskTop=QApplication::desktop();

    deskRect=deskTop->availableGeometry();

 

    normalPoint.setX(deskRect.width()-rect().width()-1);

    normalPoint.setY(deskRect.height()-rect().height());

    move(normalPoint.x(),deskRect.height()-1);

    show();

    timerShow->start(5);

}

//平滑顯示出來

void Widget::myMove()

{

    static int beginY=QApplication::desktop()->height();

    beginY--;

    move(normalPoint.x(),beginY);

    if(beginY<=normalPoint.y())

    {

        timerShow->stop();

        timerStay->start(1000);

    }

}

//停留顯示

void Widget::myStay()

{

    static int timeCount=0;

    timeCount++;

    if(timeCount>=9)

    {

        timerStay->stop();

        timerClose->start(200);

    }

}

//自動關閉時實現淡出效果

void Widget::myClose()

{

static double tran=1.0;

if(isEnter){

tran = 1.0;

setWindowOpacity(tran);

return;

}

    tran-=0.1;

    if(tran<=0.0)

    {

        timerClose->stop();

        emit close();

    }

    else

        setWindowOpacity(tran);

}

void Widget::seeInfo()

{

    

}

 

void Widget::enterEvent(QEvent *)

{

isEnter = true;

}

void Widget::leaveEvent(QEvent *)

{

    isEnter = false;

}

二、標題部分與自定義按鈕部分

//標題類 class titlewidget
#include "titlewidget.h"
#include <QLabel>
#include <QToolButton>
#include <QHBoxLayout>
#include <QPainter>
#include <QLinearGradient>
#include <QIcon>

titleWidget::titleWidget(QWidget *parent) :
    QWidget(parent)
{
    titleText=new QLabel;
    btnClose = new QToolButton(this);
	btnClose->setObjectName(QString::fromUtf8("btnClose"));
	btnClose->setToolTip(tr("關閉"));
	btnClose->setStyleSheet(QString::fromUtf8("QWidget[objectName=\"btnClose\"]{\n"
		"border-width: 0px;\n"
		"border-style: solid;\n"
		"padding: 0px;\n"
		"padding-left: 0px;\n"
		"padding-right: 0px;\n"
		"min-width: 16px;\n"
		"max-width: 16px;\n"
		"min-height: 16px;\n"
		"max-height: 16px;\n"
		"background-image: url(:/Resources/btn_close_normal.bmp);\n"
		"}\n"
		"\n"
		"QWidget[objectName=\"btnClose\"]:hover{\n"
		"background-image: url(:/Resources/btn_close_highlight.bmp);\n"
		"}\n"
		"\n"
		"QWidget[objectName=\"btnClose\"]:pressed{\n"
		"background-image: url(:/Resources/btn_close_down.bmp);\n"
		"}\n"
		""));
    connect(btnClose,SIGNAL(clicked()),this,SIGNAL(myClose()));
    QHBoxLayout *layout=new QHBoxLayout;
    layout->addWidget(titleText,0,Qt::AlignLeft);
    layout->addStretch();
    layout->addWidget(btnClose,0,Qt::AlignRight);
    layout->setMargin(0);
    setLayout(layout);
    setFixedHeight(20);
}

void titleWidget::paintEvent(QPaintEvent *)
{
    QLinearGradient linear(rect().topLeft(),rect().bottomRight());
    linear.setColorAt(0,QColor(227,207,87));
    linear.setColorAt(0.5,QColor(245,222,179));
    linear.setColorAt(1,QColor(189,252,201));

    QPainter painter(this);
    painter.setBrush(QBrush(linear));
    painter.setPen(Qt::NoPen);
    painter.drawRect(rect());
}

void titleWidget::setTitleText(QString title)
{
    titleText->setText(title);
}
//按鈕類:class mypushbutton
#include "mypushbutton.h"
#include <QPalette>
#include <QPixmap>
#include <QCursor>

myPushButton::myPushButton(QWidget *parent) :
    QPushButton(parent)
{
}
myPushButton::myPushButton(QString iconStr,QString textStr, QWidget *parent):QPushButton(parent)
{
    QPixmap pixmap(":/Resources/"+iconStr);
    setIcon(QIcon(pixmap));
    setIconSize(pixmap.size());
	setText(textStr);
    resize(pixmap.size());
    setBkPalette(0);//設定背景完全透明
    setFlat(true);
    setAutoFillBackground(true);
}

void myPushButton::setBkPalette(int transparency)//設定背景透明度
{
   QPalette palette;
   palette.setBrush(QPalette::Button,QBrush(QColor(255,255,255,transparency)));
   setPalette(palette);
}
void myPushButton::enterEvent(QEvent *)
{
	setCursor(Qt::PointingHandCursor);
}
void myPushButton::leaveEvent(QEvent *)
{
    setCursor(Qt::CustomCursor);
}
void myPushButton::mousePressEvent(QMouseEvent *e)
{
    
}
void myPushButton::mouseReleaseEvent(QMouseEvent *e)
{

	emit clicked();
}

相關推薦

Qt 實現桌面右下訊息提示

      簡單的做了一個類似QQ訊息提示的訊息彈窗提示的小模組,便於在系統後臺程式提示檢測的資訊,使用Qt開發,設計整體思路是做一個無框架的widget,自己實現標題欄和內容欄,添加了向上移出,自動消隱退出效果,窗體簡單,模組結構便於以後進行擴充套件和移植,旨在顯示文字資

PHP桌面右下自動彈提示

<!DOCTYPE html> <html>     <head>         <title></title>         <meta http-equiv="pragma" content="no-c

java實現桌面右下(模仿,類似於qq訊息

最近需要一個java實現桌面彈窗的小功能,類似於電腦桌面右下角的小廣告一樣的功能,在csdn上找到一個很好的一個,功能很多,我去除了一點不需要的程式碼,改了下外觀等。 原作者:https://www.cnblogs.com/hgxbo/p/5508384.html 修改後的

視窗右下訊息出框

<!DOCTYPE html> <html> <head> <title>視窗右下角訊息彈出框</title> <meta charset="utf-8"/> <style> *{marg

用JS寫煩人右下小廣告

啦啦啦啦,寫一下最近JS的學習進度。 最近JS學了DOM、正則、定時器… 用定時器寫了一個右下角的彈窗廣告。 先寫一下定時器的基本概念 週期定時器:setInterval(function,每間隔多少s執行) 一次定時器:setTimeout(function,間隔多少s執行一次)

Jquery實現自定義訊息

當覺得瀏覽器的alert彈框太難看時,就會想動手自行編輯彈框啦,首先在html頁面0的插入彈框div。 寫在body結束標籤前就行了。 <div id="cjy-maskBox"><

實現瀏覽器-右下-小廣告功能(您有新的訊息

<script language="javascript">     var Message={     set: function() {//最小化與恢復狀態切換     var set=this.minbtn.status == 1?[0,1,'block',this.char[0],'最小化

jQuery實現右下提示

用jQuery實現這個功能其實非常簡單,網上的例子太多太多了。不過,那些例子一般沒有涉及到後臺往前臺傳值的問題。在本文中這個問題會得到解決。 在實際應用中,提示框都是自動彈出的,因此,jQuery函式需寫在jsp頁面的<html>之外,即在<html>

Qt中給程式在桌面右下通知欄處新增圖示

今天整理程式碼的時候看到一段以前也是在網上各種搜查才找到的程式碼片段,目的是為了給程式加個圖示,然後還可以在桌面右下角的通知欄裡也加入個,並對這個圖示的一些事件進行響應!  把這一片斷程式碼與大家分享一下,希望有用!!! 程式碼如下:         //設定程式圖示 se

使用js冒泡實現點擊空白處關閉

spa add ria 點擊 on() cin 實現 eve rom 什麽是事件冒泡? 如圖:在一個對象上觸發某類事件(比如單擊onclick事件),這個事件會向這個對象的父級對象傳播,從裏到外,直至它被處理(父級對象所有同類事件都將被激活),或者它到達了

jQuery實現網頁右下懸浮層提示

off eight slide ring height containe htm enter else 最近有同事提到類似網頁右下角的消息懸浮提示框的制作。我之前也做過一個類似的例子,很簡單。是仿QQ消息。現在感覺之前的那個例子只是說了實現原理,整體上給你的感覺還是太醜,今

右下出廣告

con blog url dev cli clas click port ide 1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF

日常操作之如何將桌面右下圖標收到一起

定義 技術分享 分享 分享圖片 如何 com info 技術 -- 步驟: 1、在桌面最下面一欄點擊右鍵-----屬性 2、屬性頁面:選擇“自定義” 3、自定義界面:具體操作如圖 日常操作之如何將桌面右下角圖標收到一起

[UWP]使用Picker實現一個簡單的ColorPicker

block 可選 over face 取顏色 uwp ica 調用 name 在上一篇博文《[UWP]使用Popup構建UWP Picker》中我們簡單講述了一下使用Popup構建適用於MVVM框架下的彈窗層組件Picker的過程。但是沒有應用實例的話可能體現不出Picke

win7桌面右下提示副本不是正版怎麼辦?

win7系統現在仍然具有很大的市場佔有率,很多同學在使用時目前遇到過突然在右下角提示“副本不是正版”的提示。今天小編就為大家分享一下這種情況的解決辦法,這種情況大多是由於系統沒有用正規渠道啟用。只要啟用後更改主題就可以了。下面是最新可用的win7金鑰,希望可以幫助大家,如果金鑰失效或者被用完

Sagit.Framework For IOS 開發框架入門教程5:訊息STMsgBox

前言: 昨天剛寫了一篇IT連創業的文章:IT連創業系列:產品設計之答題模組,(歡迎大夥關注!) 感覺好久沒寫IOS的文章了,今天趁機,來補一篇,Sagit的教程。 今天主要是分享訊息彈窗功能,即STMsgBox的用法: STMsgBox為彈窗相關的功能的原始碼。 1、對外API功能呼叫說明:

popwin實現仿京東 商品列表 條件篩選 列表

有需求就有加班寫一個仿照京東的條件篩選彈窗,按道理講 Drawerlayout, dialog  ,彈窗activity  ,popwin  都可以實現的,看自己擅長什麼,或者專案適合什麼 就用什麼寫就OK;我是選擇用popwin寫的, 因為正好之前別的寫了彈窗選擇的對話方塊

Qt實現桌面截圖

實現桌面截圖,軟體如下: 圖1 桌面截圖 主要介紹一個方法 grabWindow(WId window, int x = 0, int y = 0, int width = -1, int h

電腦桌面右下出現測試模式 Windows7內部版本

新裝系統啟用後,過段時間桌面右下角出現“測試模式 Windows 7 內部版本 7601“字樣,這是工程師測試模式,把不顯示這個提示的方法分享一下! 1. 點選開始選單,然後選擇所有程式! 2. 在所有程式中找到附件! 3. 在附件中找到命令提示符,然後右鍵點選,選擇以管理

怎麼將桌面右下的小圖示隱藏

滑鼠在桌面最下面的導航———>右擊滑鼠(彈出選單)———>(最下面)屬性———>工作列和[開始]選單屬性———>通知區域(單擊自定義)———>去掉(始終在選單欄上顯示所有圖示和通知(A))複選框之前的對號 OK,可以將所有的圖示隱藏到小三角里面