[Qt6][QML][教程]自定义托盘右键菜单自动隐藏

上节我们创建了个比较现代简约风的托盘右键菜单,这节我们来讲讲怎么实现其自动关闭。

教程被应用在MediaStateT中

MediaStateT Github项目地址: https://github.com/taxue-alfred/MediaStateT

MediaStateT Gitee项目地址: https://gitee.com/MediaState/MediaStateT

毋庸置疑,我们需要一个Timer,然后比较关键的来了,我们怎么知道我们的鼠标放没放在窗口上?MouseArea?并不是!这家伙只能在最上层的时候被识别到。那每个控件都放个MouseArea不就可以实现鼠标的位置检测了?确实可以。但是比PPT还卡的应用,你用吗?

解决思路

临机一动,我的五个控件都是由Button控件组成的,Button控件有一个叫hovered的属性,可以返回鼠标有没有在按钮上,那么就没必要检测鼠标的位置了,简单又方便

Timer{
    
    
        id:close_timer
        interval:800
        repeat:true
        running:true
        onTriggered:{
    
    
            root_window.show()
            close_timer.restart()
            //检测鼠标是不是在Button上
            if(!start_with_system.hovered && !showWindow.hovered &&
                !showFrequency.hovered && !connectMSBoard.hovered &&
                !exit.hovered){
    
    
                    root_window.close()
            }
        }
    }

效果展示

其他的上下节的文章看我的这个Qt收录吧,。。实在是懒得写上下节了。

https://blog.csdn.net/qq_38844263/category_11578713.html

猜你喜欢

转载自blog.csdn.net/qq_38844263/article/details/122618081