【Qt】实现一个相同文案过滤的气泡弹窗

思路:

我们首先遍历m_mBubbleTextForTimerIndex,查找是否存在同样的文案。如果找到了相同的文案,我们会先删除旧的定时器索引项,然后创建一个新的定时器,并将文案和对应的弹窗项和定时器索引添加到m_mBubbleTextForTimerIndexm_mBubbleWidgetTimerIndex中。

如果没有找到相同的文案,我们会继续执行原本的逻辑,创建新的弹窗项和定时器,并将它们添加到对应的映射中。

这样做的效果是,如果同样的文案进来,它会刷新上一次出现的弹窗的显示时间;如果是新的文案,它会创建新的弹窗项和定时器。

void ZRUiBubble::AddBubbleItem(BubbleType type, const QString& text)
{
    // 查找已有的同样文案的定时器索引
    std::map<int, QString>::iterator iter = m_mBubbleTextForTimerIndex.begin();
    for (; iter != m_mBubbleTextForTimerIndex.end(); ++iter)
    {
        if (iter->second == text)
        {
            // 更新定时器的时间
            int timerIndex = iter->first;
            killTimer(timerIndex);
            int newTimerIndex = startTimer(kiBubbleShowTime);
            m_mBubbleTextForTimerIndex[newTimerIndex] = iter->second;
            m_mBubbleWidgetTimerIndex[newTimerIndex] = m_mBubbleWidgetTimerIndex[timerIndex];
            
            // 删除旧的定时器索引项
            m_mBubbleTextForTimerIndex.erase(iter);
            m_mBubbleWidgetTimerIndex.erase(timerIndex);
            return;
        }
    }
    
    // 创建新的弹窗项和定时器
    QHBoxLayout* bubbleItemLayout = CreateBubbleItem(type, text);
    if (bubbleItemLayout == nullptr)
    {
        ST_STF::STF_LOG_ERROR("Create Bubble Item Failed !");
        return;
    }
    m_pVBoxLayout->addLayout(bubbleItemLayout);
    int timerIndex = startTimer(kiBubbleShowTime);
    m_mBubbleWidgetTimerIndex[timerIndex] = m_vBubbleWidget.back();
    m_mBubbleTextForTimerIndex[timerIndex] = text;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Monster_H777

我直说吧:你的奖励我的动力~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值