您當(dāng)前的位置 :環(huán)球傳媒網(wǎng)>健康 > 正文
QT托盤消息模擬QQ消息通知_播報(bào)
2023-05-30 16:10:51
來源:QT教程
(資料圖片僅供參考)
使用QQ時,當(dāng)有消息過來時,托盤的圖標(biāo)一閃一閃的,鼠標(biāo)懸浮時出現(xiàn)消息通知列表,顯示名字和數(shù)量,Qt有自帶的消息托盤,那沒有鼠標(biāo)懸浮事件,這里通知定時器查找鼠標(biāo)的坐標(biāo),在托盤范圍內(nèi)時顯示消息列表,離開托盤范圍時隱藏列表,消息列表采用Qt的Model、View加自定義委托實(shí)現(xiàn),代碼實(shí)現(xiàn)如下:
#ifndef ITEMDEFINE_H#define ITEMDEFINE_H#include #include #include #define PrintTime (QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:"))//托盤消息結(jié)構(gòu)typedef struct STTrayInfo{QString strAvatar{""};//頭像QString strName{""};int unReadCount{0};//未讀數(shù)}TrayInfo;Q_DECLARE_METATYPE(TrayInfo)#endif // ITEMDEFINE_H
#ifndef CITEMDELEGATE_H#define CITEMDELEGATE_H#include #include #include class CItemDelegate : public QStyledItemDelegate{Q_OBJECTpublic:explicit CItemDelegate(QObject *parent = nullptr);~CItemDelegate();//重畫函數(shù)void paint(QPainter * painter,const QStyleOptionViewItem & option,const QModelIndex & index) const;QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;private:QFont font = QFont("Microsoft YaHei");};#endif // CITEMDELEGATE_H
#include ""#include #include #include #include #include #include #include ""CItemDelegate::CItemDelegate(QObject *parent) :QStyledItemDelegate(parent){(10);}CItemDelegate::~CItemDelegate(){}void CItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{if(()){painter->save();QVariant variant = (Qt::UserRole+1);//根據(jù)角色取值TrayInfo data = ();QStyleOptionViewItem viewOption(option);//用來在視圖中畫一個itemQRectF rect;(());(());(()-1);(()-1);if((QStyle::State_Selected))//選中畫背景{painter->setPen(QPen(Qt::blue));painter->setBrush(QColor(229, 241, 255));painter->drawRect(rect);}else if((QStyle::State_MouseOver))//鼠標(biāo)懸浮畫背景{painter->setPen(QPen(Qt::green));painter->setBrush(QColor(189, 193, 217));//bdc1d9painter->drawRect(rect);}// else{//默認(rèn)狀態(tài)// painter->setPen(QPen(Qt::gray));// painter->setBrush(Qt::NoBrush);// painter->drawRect(path);// }//畫頭像QPixmap pix();if(())(":/image/system_");pix = (30, 30, Qt::KeepAspectRatio, Qt::SmoothTransformation);QRect imageRect = QRect(() + 5, ()+5, 30, 30);painter->drawPixmap(imageRect, pix);//畫名字QFontMetrics fontMetrics(font);//如果當(dāng)前字體下,字符串長度大于100QString fileName = ;if((fileName) >150){fileName = QFontMetrics(font).elidedText(fileName, Qt::ElideRight, 150);}QRect NameRect = QRect(() + 40, ()+10, 150, 18);painter->setRenderHints(QPainter::Antialiasing|QPainter::TextAntialiasing, true);//抗鋸齒painter->setPen(QPen(Qt::blue));painter->setFont(font);painter->drawText(NameRect, Qt::AlignLeft, fileName);//畫圓QRect circle = QRect(() + 200, ()+5, 30, 20);painter->setBrush(Qt::red);//畫填充painter->setPen(QPen(Qt::red));painter->drawRoundedRect(circle, 10, 10, Qt::RelativeSize);//圓角矩形//畫統(tǒng)計(jì)數(shù)字QRect UnReadRect = QRect(() + 200, ()+5, 30, 20);painter->setPen(QPen(Qt::white));painter->setFont(font);painter->drawText(UnReadRect, Qt::AlignCenter, QString("%1").arg());painter->restore();}}QSize CItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const{return QSize(260, 40);}
#ifndef CNOTICELIST_H#define CNOTICELIST_H#include #include #include ""#include ""#include #include namespace Ui {class CNoticeList;}class CNoticeList : public QWidget{Q_OBJECTpublic:explicit CNoticeList(QWidget *parent = nullptr);~CNoticeList();void initView();void initData();void initConnect();void initModelData();void setWidgetHeight();int rowCount();void deleteListItem(const TrayInfo &sessionInfo);void changeListItem(const TrayInfo &sessionInfo);public slots:void slotItemClicked(const QModelIndex &index);void slotIgnoreAll();protected:void mouseReleaseEvent(QMouseEvent *event);private:Ui::CNoticeList *ui;QStandardItemModel *m_model{nullptr};CItemDelegate *m_delegate{nullptr};int m_nMessageTotal{0};QMutex m_MutexNotice;QMap>m_mapMsgid;};#endif // CNOTICELIST_H
#include ""#include "ui_"#include #include #include #include #include CNoticeList::CNoticeList(QWidget *parent) :QWidget(parent),ui(new Ui::CNoticeList){ui->setupUi(this);setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint | Qt::WindowStaysOnTopHint //不顯示菜單欄| Qt::Tool);//不顯示任務(wù)欄圖標(biāo)initView();initData();initConnect();}CNoticeList::~CNoticeList(){delete ui;}void CNoticeList::initView(){ui->pushButton_ignoreAll->setStyleSheet("QPushButton{""font-family: \"Microsoft YaHei\";""font-size: 12px;""color: blue;}");ui->label_newMesage->setStyleSheet("QLabel{font-family: \"Microsoft YaHei\";font-size: 12px;}");ui->label_count->setStyleSheet("QLabel{font-family: \"Microsoft YaHei\";font-size: 12px;}");ui->listView->verticalScrollBar()->setStyleSheet("QScrollBar:vertical{width:8px;background:rgba(0,0,0,0%);""margin:0px,0px,0px,0px;padding-top:9px;padding-bottom:9px;}"http:// 留出9px給上面和下面的箭頭"QScrollBar::handle:vertical{width:8px;background:rgba(0,0,0,25%);""border-radius:4px;min-height:20;}"http:// 滾動條兩端變成橢圓"QScrollBar::handle:vertical:hover{width:8px;background:rgba(0,0,0,50%);""border-radius:4px;min-height:20;}");// 鼠標(biāo)放到滾動條上的時候,顏色變深ui->listView->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);ui->listView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);}void CNoticeList::initData(){m_delegate = new CItemDelegate();m_model = new QStandardItemModel();ui->listView->setItemDelegate(m_delegate);ui->listView->setModel(m_model);ui->listView->setSpacing(0);initModelData();}void CNoticeList::initConnect(){connect(ui->listView, &QListView::clicked, this, &CNoticeList::slotItemClicked);connect(ui->pushButton_ignoreAll, &QPushButton::clicked, this, &CNoticeList::slotIgnoreAll);}void CNoticeList::initModelData(){QStringListModel *stringlistModel = new QStringListModel(QColor::colorNames(), this);//獲取顏色值QStringList colorList = stringlistModel->stringList();for (int i = 1; i < 7; ++i){QStandardItem *Item = new QStandardItem;TrayInfo itemData; = QString(":/image/%").arg(i); = colorList[i]; = 1;m_nMessageTotal += 1;Item->setData(, Qt::UserRole); // 單一存取 這個用來匹配過濾條件Item->setData(QVariant::fromValue(itemData), Qt::UserRole+1);//整體存取m_model->insertRow(0, Item); //追加Item}setWidgetHeight();}void CNoticeList::setWidgetHeight(){//刷新消息數(shù)量if(m_nMessageTotal >0)ui->label_count->setText(QString("%1").arg(m_nMessageTotal));elseui->label_count->setText("");if(m_model->rowCount() < 5){ui->listView->setMinimumHeight(m_model->rowCount() * 40 + 4);ui->listView->setMaximumHeight(m_model->rowCount() * 40 + 4);this->setMinimumHeight(ui->listView->height() + 60);this->setMaximumHeight(ui->listView->height() + 60);}else{ui->listView->setMinimumHeight(204);ui->listView->setMaximumHeight(204);this->setMinimumHeight(264);this->setMaximumHeight(264);}}int CNoticeList::rowCount(){if(nullptr == m_model)return 0;elsem_model->rowCount();}void CNoticeList::mouseReleaseEvent(QMouseEvent *event){if(event->button() == Qt::LeftButton)this->hide();}void CNoticeList::deleteListItem(const TrayInfo &info){for(int i = 0; i < m_model->rowCount(); ++i){QStandardItem *Item = m_model->item(i);QString strName = Item->data(Qt::UserRole).toString();if(0 == ()){QVariant variant = Item->data(Qt::UserRole+1);//根據(jù)角色取值TrayInfo data = ();m_nMessageTotal -= ;//總消息數(shù)減去這個會話消息數(shù)if(m_nMessageTotal < 0)m_nMessageTotal = 0;m_model->removeRow(i);break;}}setWidgetHeight();}void CNoticeList::slotItemClicked(const QModelIndex &index){QMutexLocker locker(&m_MutexNotice);QVariant variant = (Qt::UserRole+1);//根據(jù)角色取值TrayInfo data = ();//刪除這個itemdeleteListItem(data);//隱藏自己this->hide();}void CNoticeList::slotIgnoreAll(){QMutexLocker locker(&m_MutexNotice);if(m_model->rowCount() >0){m_model->clear();m_nMessageTotal = 0;this->hide();}}
#ifndef CUSTOMSYSTEMTRAY_H#define CUSTOMSYSTEMTRAY_H#include #include #include #include #include #include ""#include ""#include ""class QAction;class QMemu;class QSystemTrayIcon;class QWidget;class QWidgetAction;class QPushButton;class QToolButton;class QLabel;class QHBoxLayout;class QPainter;class QTimer;class CustomSystemTray : public QSystemTrayIcon{Q_OBJECTpublic:explicit CustomSystemTray(QObject *parent = nullptr);void initData();void checkMousePosition();//Qt的托盤沒有懸停事件,通過鼠標(biāo)光標(biāo)位置來判斷懸浮事件void initConnect();public slots:void slotMessageNotice();void slotFlashTimerOut();private:QTimer *m_pMessageNotice{nullptr};//模擬消息通知QTimer *m_pTimerFlash{nullptr};//圖標(biāo)閃爍int m_flashCount{0};CNoticeList *m_noticList{nullptr};};#endif // CUSTOMSYSTEMTRAY_H
#include ""#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include CustomSystemTray::CustomSystemTray(QObject *parent) :QSystemTrayIcon(parent){initData();}void CustomSystemTray::initData(){m_noticList = new CNoticeList();m_pTimerFlash = new QTimer;connect(m_pTimerFlash, &QTimer::timeout, this, &CustomSystemTray::slotFlashTimerOut);m_pTimerFlash->start(500);}void CustomSystemTray::checkMousePosition(){//托盤位置24X40QRect currentRect = geometry();int xMin = ();int xMax = () + ();int yMin = ();int yMax = () + ();int x = QCursor::pos().x();int y = QCursor::pos().y();if(xMin<=x && x<=xMax && yMin<=y && y<=yMax){QRect menuRect;(xMin);(yMin - m_noticList->height());(m_noticList->width());(m_noticList->height());m_noticList->setGeometry(menuRect);if(m_noticList->rowCount() >0)m_noticList->show();elsem_noticList->hide();}else if((nullptr != m_noticList) && (!m_noticList->isHidden())){//如果是顯示的消息框,鼠標(biāo)在顯示框范圍內(nèi)不隱藏int pxMin = xMin;int pxMax = xMin + m_noticList->width();int pyMin = yMin - m_noticList->height();int pyMax = yMin;if(pxMin<=x && x<=pxMax && pyMin<=y && y<=pyMax)m_noticList->show();elsem_noticList->hide();}}void CustomSystemTray::initConnect(){// 此處使用定時器模擬消息的發(fā)送m_pMessageNotice = new QTimer;connect(m_pMessageNotice, &QTimer::timeout, this, &CustomSystemTray::slotMessageNotice);m_pMessageNotice->start(3000);}void CustomSystemTray::slotMessageNotice(){m_flashCount = 0;if (m_pTimerFlash == nullptr){m_pTimerFlash = new QTimer;connect(m_pTimerFlash, &QTimer::timeout, this, &CustomSystemTray::slotFlashTimerOut);m_pTimerFlash->start(500);}}void CustomSystemTray::slotFlashTimerOut(){if(m_noticList->rowCount() < 1){this->setIcon(QIcon(":/image/"));return;}//qDebug() << "slotFlashTimerOut============m_flashCount=====" << m_flashCount;// 消息通知,使用定時器刷新圖標(biāo)m_flashCount++;checkMousePosition();if (m_flashCount % 2 == 1){this->setIcon(QIcon(":/image/"));}else{this->setIcon(QIcon(":/image/"));}}
運(yùn)行效果
【領(lǐng) QT開發(fā)教程 學(xué)習(xí)資料, 點(diǎn)擊下方鏈接莬費(fèi)領(lǐng)取↓↓ ,先碼住不迷路~】
點(diǎn)擊這里:
關(guān)鍵詞:
相關(guān)閱讀
版權(quán)和免責(zé)申明
凡注有"環(huán)球傳媒網(wǎng)"或電頭為"環(huán)球傳媒網(wǎng)"的稿件,均為環(huán)球傳媒網(wǎng)獨(dú)家版權(quán)所有,未經(jīng)許可不得轉(zhuǎn)載或鏡像;授權(quán)轉(zhuǎn)載必須注明來源為"環(huán)球傳媒網(wǎng)",并保留"環(huán)球傳媒網(wǎng)"的電頭。
- 中華人民共和國人口與計(jì)劃生育法第三十七條是什么內(nèi)容?計(jì)劃生育的優(yōu)勢是什么?
- 網(wǎng)易云游戲光遇實(shí)名認(rèn)證怎么解除? 光遇如何修改實(shí)名認(rèn)證?
- 股票跌停怎么辦?一字跌停是什么意思?
- 假釋官的含義是什么? 假釋出獄是什么意思?
- 新硬盤如何分區(qū)?電腦新裝硬盤怎樣激活使用?
- 湖南都市頻道都市調(diào)解室是什么?民生欄目的發(fā)展歷程是?
- 被公司辭退要開什么證明?員工能力不足被開除有賠償嗎?
- 筆的發(fā)明介紹 毛筆是誰發(fā)明的?
- 信用卡逾期后怎么協(xié)商只還本金?銀行卡掛賬停息是什么意思?
- 你知道如何查詢手機(jī)剩余流量嗎?中國聯(lián)通怎么查流量?
資訊
- 服務(wù)器哪個便宜_今日熱搜
- 天津財(cái)經(jīng)大學(xué)是公辦類大學(xué)嗎?天津財(cái)經(jīng)大學(xué)詳細(xì)地址在哪?
- 誰是曼城在下賽季英超的最大對手?基恩:我會選擇曼聯(lián)
- 迪阿股份:暫未涉及培育鉆石業(yè)務(wù)
- 存款利率上調(diào)意味著什么?大額存單利率下降會導(dǎo)致什么?
- 童心向黨 快樂成長 | 興義市8名運(yùn)動員在2023年貴州省少兒武術(shù)大賽上斬獲佳績-焦點(diǎn)信息
- 神舟十六號飛船已完成五次自主變軌
- 金融風(fēng)暴是什么意思?金融風(fēng)暴會有什么影響?
- 交叉盤什么意思(股票做交叉盤意味什么)
- 每日消息!最驚悚神秘小說精選集(對于最驚悚神秘小說精選集簡單介紹)
焦點(diǎn)
- 2411是哪里的區(qū)號?區(qū)號是怎么劃分的?
- 中國銀行大額存單有風(fēng)險(xiǎn)嗎 大額存單有什么坑?
- 怎么寫簡歷才能讓HR一眼看中?HR看簡歷究竟看了些什么?
- 鶴壁市市長趙宏宇到河南能源化工集團(tuán)鶴煤公司調(diào)研_熱資訊
- 藥物分析是什么學(xué)科?藥品檢驗(yàn)工作的基本程序是什么?
- 山西2023年第四批50家售電公司重大信息變更
- 兩陰夾一陽是什么?兩陰夾一陽的相關(guān)解析
- WINMax22023正式開啟預(yù)售了嗎?售價(jià)是多少呢?
- 廟會的起源是什么? 元宵節(jié)逛廟會的由來是什么?
- 封閉式基金交易規(guī)則是什么?封閉期基金虧光了怎么辦?