A sliding menu modeled on the word interface (2) (retractable drop-down box)

There are two processes to be done here: 1. Up and down of widgets and 2. Stretching animation of widgets.

Floating and lower layers can be controlled by setting widget properties:

    if(isfloating)
    {
        m_is_floating = true;
        setWindowFlags(Qt::FramelessWindowHint | Qt::ToolTip);
    }
    else
    {
         m_is_floating = false;
        setWindowFlags(Qt::FramelessWindowHint | Qt::Widget);

  Qt::WindowFlags:

        Then we must talk about the setting of the window ID of the control. The window identification consists of two parts: window type and window hint. A window has only one window type. The window prompt defines the appearance of the window, there can be multiple prompts, and the window prompt can be taken by bit or.


    To clarify the Qt::WindowFlags enumeration type, two concepts must be clear: window and widget (so the division is easy to distinguish) 


    Window: Relatively independent, there are child windows and parent windows. The main feature is that the window has a complete frame. Components that are not embedded in other components are called windows. General windows have a frame and a title bar. 


    Components: must depend on the parent window to exist, the dependency is strong, appear inside the interface of the parent form, they are embedded in other windows.
    With the above concepts, it is easy to explain the usage of the Qt::WindowFlags enumeration type. First, determine whether a control is a form according to the specification of the parameter parent. If the value is 0, then it can be judged as a window. Otherwise, it is necessary to judge whether it is a window or a component according to the value of the Qt::WindowFlags enumeration type.

The following is an explanation of the translated official document, but the goose is not very clear;

Qt::Widget  0x00000000   这是QWidget的默认类型。这种类型的窗口小部件是子窗口小部件(如果它们有父窗口),独立窗口(如果它们没有父窗口)。参见Qt::Window和Qt::SubWindow。

Qt::Window  0x00000001     表示小部件是一个窗口,通常带有一个窗口系统框架和一个标题栏,不管小部件是否有父窗口。注意,如果小部件没有父组件,则不可能取消设置此标志。

Qt::Dialog  0x00000002   窗口表示小部件是一个应该装饰为对话框的窗口(即,通常标题栏中没有最大化或最小化按钮)。这是QDialog的默认类型。如果您想使用它作为一个模态对话框,则应该从另一个窗口启动它,或者有一个父窗口并与QWidget::windowModality属性一起使用。如果将其设置为模态,则对话框将阻止应用程序中的其他顶级窗口获得任何输入。我们将具有父窗口的顶级窗口称为辅助窗口。

Qt::Sheet   0x00000004    窗口表示该窗口是Macintosh工作表。由于使用工作表意味着窗口模式,建议使用QWidget::setWindowModality()或QDialog::open()。

Qt::Drawer  0x00000006    窗口表示小部件是Macintosh的drawer。

Qt::Popup   0x00000008    窗口表示小部件是一个弹出的顶级窗口,即它是模态的,但是有一个适合弹出菜单的窗口系统框架。

Qt::Tool   0x0000000a     窗口表示小部件是一个工具窗口。工具窗口通常是一个比通常标题栏和装饰小的窗口,通常用于工具按钮的集合。如果有父类,工具窗口将始终保持在它的顶部。如果没有父类,也可以考虑使用Qt::WindowStaysOnTopHint。如果窗口系统支持它,工具窗口可以用稍微轻一些的框架装饰。它还可以与Qt::FramelessWindowHint相结合。
在Mac OS X上,工具窗口对应于窗口的浮动类。这意味着窗口位于正常窗口之上的一层;在它上面放一个普通的窗户是不可能的。默认情况下,当应用程序处于非活动状态时,工具窗口将会消失。这可以由Qt::WA_MacAlwaysShowToolWindow属性控制。

Qt::ToolTip      0x0000000c  窗口表示小部件是一个工具提示。这在内部用于实现工具提示。

Qt::SplashScreen  0x0000000e   窗口表示该窗口是一个闪屏。这是QSplashScreen的默认类型。

Qt::Desktop  0x00000010   窗口表示这个小部件就是桌面。这是QDesktopWidget的类型。

Qt::SubWindow 0x00000012表示此小部件是子窗口,例如QMdiSubWindow小部件。

 Look at this:

Qt::Widget               //是一个窗口或部件,有父窗口就是部件,没有就是窗口
Qt::Window               //是一个窗口,有窗口边框和标题
Qt::Dialog               //是一个对话框窗口
Qt::Sheet                //是一个窗口或部件Macintosh表单
Qt::Drawer               //是一个窗口或部件Macintosh抽屉
Qt::Popup                //是一个弹出式顶层窗口
Qt::Tool                 //是一个工具窗口
Qt::ToolTip              //是一个提示窗口,没有标题栏和窗口边框
Qt::SplashScreen         //是一个欢迎窗口,是QSplashScreen构造函数的默认值
Qt::Desktop              //是一个桌面窗口或部件
Qt::SubWindow            //是一个子窗口 //为窗口添加一些功能
Qt::CustomizeWindowHint          //关闭默认窗口标题提示
Qt::WindowTitleHint              //为窗口修饰一个标题栏
Qt::WindowSystemMenuHint         //为窗口修饰一个窗口菜单系统
Qt::WindowMinimizeButtonHint     //为窗口添加最小化按钮
Qt::WindowMaximizeButtonHint     //为窗口添加最大化按钮
Qt::WindowMinMaxButtonsHint      //为窗口添加最大化和最小化按钮
Qt::WindowCloseButtonHint
Qt::WindowContextHelpButtonHint
Qt::MacWindowToolBarButtonHint
Qt::WindowFullscreenButtonHint
Qt::BypassGraphicsProxyWidgetQt::WindowShadeButtonHint
Qt::WindowStaysOnTopHintQt::WindowStaysOnBottomHint
Qt::WindowOkButtonHintQt::WindowCancelButtonHint
Qt::WindowTransparentForInput

That's it for WindowFlags, here are the scaling effects:

Mainly used QT's QPropertyAnimation property (the essence of the animation effect is to use a timer to interpolate a change action and display the process).

The transformation movement here is the height of the Widget

    Q_PROPERTY(int flexhight READ flexhight WRITE setflexhight)//Add a high attribute

    animation = new QPropertyAnimation(this, "flexhight"); //initialize the animation class
    animation->setDuration(50); //set the transformation speed (50)
    animation->setEasingCurve(QEasingCurve::InQuad); (here I understand it as transformation Type of acceleration (fast first and then slow)

The source code is as follows:

widget.h

#ifndef ADAPTER_WIDGET_H
#define ADAPTER_WIDGET_H
#include <QPropertyAnimation>
#include <QPushButton>
#include <QPainter>
#include <QColor>
#include <QBrush>
#include <QLayout>
#include <QWidget>

class QStackedWidget;
class PushButton;
class AdapterWidget : public QWidget
{
    Q_OBJECT
    Q_PROPERTY(int flexhight READ flexhight WRITE setflexhight)
 public:
    explicit AdapterWidget(QWidget *parent = 0);
    ~AdapterWidget();

    int flexhight()
    {
        return this->height();
    }
    void setflexhight(int heigh)
    {
        setFixedHeight(heigh);
    }
    void addWidget(QWidget *);
    void setCurrentIndex(int);
    void set_stretch();
    void set_shrink();
    bool status();

    bool is_floating();
    void set_floating(bool);
signals:
    void signal_adapter_floating(bool);

public slots:
    void slot_amotion();
    void slot_floating_click();
private:
    QPropertyAnimation *animation;
    QStackedWidget *m_stackewidget;
    bool m_is_floating;
    PushButton *m_floating_button;
};

#endif // ADAPTER_WIDGET_H

widget.cc

#include "adapter_widget.h"
#include <QStackedWidget>
#include <QHBoxLayout>
#include "defwidgets/pushbutton.h"
AdapterWidget::AdapterWidget(QWidget *parent):
    QWidget(parent),
    m_is_floating(false)
{
    setParent(parent);
    m_stackewidget = new QStackedWidget(this);
    m_floating_button = new PushButton("float" ,this);
    setFixedHeight(51);
    setWindowFlags(Qt::FramelessWindowHint);
    animation = new QPropertyAnimation(this, "flexhight");
    animation->setDuration(50);
    animation->setEasingCurve(QEasingCurve::InQuad);
    QHBoxLayout *hlayout = new QHBoxLayout;
    hlayout->setSpacing(0);
    hlayout->setContentsMargins(0, 0, 0, 0);
    hlayout->addWidget(m_stackewidget);
    hlayout->addWidget(m_floating_button);
    hlayout->setStretch(0, 20);
    hlayout->setStretch(1, 1);
    setLayout(hlayout);
    connect(m_floating_button, SIGNAL(clicked()), this, SLOT(slot_floating_click()));
}

AdapterWidget::~AdapterWidget()
{
}

void AdapterWidget::addWidget(QWidget *widget)
{
    m_stackewidget->addWidget(widget);
}

void AdapterWidget::setCurrentIndex(int index)
{
    m_stackewidget->setCurrentIndex(index);
}

void AdapterWidget::set_stretch()
{
}

void AdapterWidget::set_shrink()
{
}

bool AdapterWidget::status()
{
    return height() > 50 ? true : false;
}

bool AdapterWidget::is_floating()
{
    return m_is_floating;
}

void AdapterWidget::set_floating(bool isfloating)
{
    if(isfloating)
    {
        m_is_floating = true;
        setWindowFlags(Qt::FramelessWindowHint | Qt::ToolTip);
    }
    else
    {
         m_is_floating = false;
        setWindowFlags(Qt::FramelessWindowHint | Qt::Widget);
    }
}


void AdapterWidget::slot_amotion()
{
    if (height() > 50)
    {
        animation->setEndValue(0);
    }
    else
    {
        animation->setEndValue(51);
    }
    animation->start();
}

void AdapterWidget::slot_floating_click()
{
    if(m_is_floating)
    {
        set_floating(false);
    }
    else
    {
        set_floating(true);
    }
    emit signal_adapter_floating(m_is_floating);
}

 Code source address: https://download.csdn.net/download/mario_z/11224710

 

Guess you like

Origin blog.csdn.net/Mario_z/article/details/90763419