• <rt id="2wkqu"><small id="2wkqu"></small></rt>
  • 您當(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)"的電頭。

    Copyright ? 1999-2017 cqtimes.cn All Rights Reserved 環(huán)球傳媒網(wǎng)-重新發(fā)現(xiàn)生活版權(quán)所有 聯(lián)系郵箱:8553 591@qq.com
    久久激情五月网站,一本色道综合亚洲精品精品,午夜电影久久久久久,中文无码AV片在线 成a在线观看视频播放 婷婷色中文在线观看
  • <rt id="2wkqu"><small id="2wkqu"></small></rt>