zoukankan      html  css  js  c++  java
  • QT失去focus后自动隐藏界面

     自己开发了一个股票智能分析软件,功能很强大,需要的点击下面的链接获取:

    https://www.cnblogs.com/bclshuai/p/11380657.html

    1.应用场景

    在一些临时显示的界面,比如声音的调节面板,设置好声音后,不需要手动按钮来控制隐藏,而是离开界面,或者失去focus之后自动隐藏窗口。如下图所示。

    2.实现方法

    重写下面两个虚函数,实现离开界面和失去foucs时自动隐藏的功能

    void leaveEvent(QEvent *e);                      //离开QWidget瞬间事件
        void focusOutEvent(QFocusEvent *event);//失去focus时自动隐藏

    头文件

    #ifndef VOICECTRL_H
    #define VOICECTRL_H
    
    #include <QDialog>
    #include "ui_voicewin.h"
    class VoiceCtrl : public QDialog
    {
        Q_OBJECT
    
    public:
        VoiceCtrl();
        ~VoiceCtrl();
        Ui::VoiceCtrl ui;
    protected:
        void enterEvent(QEvent *e);                      //进入QWidget瞬间事件
        void leaveEvent(QEvent *e);                      //离开QWidget瞬间事件
        void focusOutEvent(QFocusEvent *event);//失去focus时自动隐藏
    private:
    };
    
    #endif // VOICECTRL_H

    源文件

    #include "VoiceCtrl.h"
    
    VoiceCtrl::VoiceCtrl()
    {
        ui.setupUi(this);
        setWindowModality(Qt::NonModal);
        setWindowFlags(Qt::FramelessWindowHint| Qt::WindowStaysOnTopHint);
        //setAttribute(Qt::WA_TranslucentBackground, true);//背景设置透明
        this->resize(36, 112);
        connect(ui.checkBoxVolume, &QCheckBox::clicked, this, [=]() {
        if (ui.checkBoxVolume->isChecked())
        {
            ui.horizontalSliderSimilarity->setValue(0);
        } 
        else
        {
            ui.horizontalSliderSimilarity->setValue(50);
        }
        });
        connect(ui.horizontalSliderSimilarity, &QSlider::valueChanged, this, [=]() {
            if (ui.horizontalSliderSimilarity->value()==0)
            {
                ui.checkBoxVolume->setChecked(true);
            }
            else
            {
                ui.checkBoxVolume->setChecked(false);
            }
        });
        
    }
    
    VoiceCtrl::~VoiceCtrl()
    {
        
    }
    void VoiceCtrl::enterEvent(QEvent *e)
    {
        show();
    }
    
    void VoiceCtrl::leaveEvent(QEvent *e)//离开隐藏closebutton
    {
        hide();
    }
    
    void VoiceCtrl::focusOutEvent(QFocusEvent * event)
    {
        //如果是点击界面上的控件,声音控制界面也会失去focus,所以点击界面上的控件时,这里返回不隐藏
        if (ui.checkBoxVolume->hasFocus()||ui.horizontalSliderSimilarity->hasFocus())
        {
            return;
        }
        hide();
    }

    3.创建对象并使用

    在需要使用声音调节面板的地方,创建VoiceCtrl对象,通过点击按钮,来显示声音控制面板到按钮的上方,离开控制面板或失去focus之后自动隐藏。

    //显示声音调节界面
        {
            QPoint point = this->mapToGlobal(ui.PlayWndToolbar_RightWnd->pos());
            m_volumeCtrl.move(point.x()-4, point.y() - 110);
            m_volumeCtrl.show(); 

    //最关键的一步,一定要将界面对象设置为该属性,否则无法实现点击其他地方,声音界面无focus时自动隐藏。
            m_volumeCtrl.setFocus(Qt::ActiveWindowFocusReason); 
        }

    ActiveWindowFocusReason说明

    public static final Qt.FocusReason ActiveWindowFocusReason

    The window system made this window either active or inactive.


    自己开发了一个股票智能分析软件,功能很强大,需要的点击下面的链接获取: https://www.cnblogs.com/bclshuai/p/11380657.html
  • 相关阅读:
    Spark5
    Spark4
    Spark3
    冲刺周期二--站立会议01
    团队第一阶段绩效考核
    各个小组对“躲避小球”游戏的评价
    软件项目第一个Sprint评论
    丹佛机场行李处理系统
    冲刺周期一--站立会议07
    冲刺周期一--站立会议06
  • 原文地址:https://www.cnblogs.com/bclshuai/p/14928429.html
Copyright © 2011-2022 走看看