Qt interface design – sidebar hiding and sliding out

In daily projects, the sidebar is often used in the layout of the interface. Putting controls on the sidebar for combined use can realize the hiding and sliding of the sub-function interface. The effect is shown as follows:

The interface controls are very simple. The main interface QWidget and the sidebar are combined with a QWidget and a button QPushbutton. Show and hide the sidebar with the click of a button. The main use is the move() function of the control, which cooperates with QPropertyAnimation to realize the sliding display and hiding of the animation effect. The animation slide-out animation effect uses the setEasingCurve() function of the QPropertyAnimation class. Different animation effects can be achieved by setting function parameters. The specific effects can be queried through the help file of Qt Create.

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C ++ language foundation, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming , QT project actual combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓

Mainwindow.h source code:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPropertyAnimation>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QPropertyAnimation *m_propertyAnimation;
    QPropertyAnimation *m_propertyAnimation2;
    bool m_bSideflag = false; private slots:
    void on_pushButton_clicked();

};

#endif // MAINWINDOW_H

Mainwindow.cpp source code:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->widget_side->move(-ui->widget_side->width(),0);// dock on the left
    ui->pushButton->move(-1,ui->widget_side->height()/2);
    m_propertyAnimation = new QPropertyAnimation(ui->widget_side,"geometry");
    m_propertyAnimation->setEasingCurve(QEasingCurve::InOutSine);
    m_propertyAnimation->setDuration(800);
    m_propertyAnimation2 = new QPropertyAnimation(ui->pushButton,"geometry");
    m_propertyAnimation2->setEasingCurve(QEasingCurve::InOutSine);
    m_propertyAnimation2->setDuration(800);
}

MainWindow::~MainWindow()
{
    delete ui;
}void MainWindow::on_pushButton_clicked()
{
    //show sidebar
    if(!m_bSideflag)
    {
        m_propertyAnimation->setStartValue(QRect(-this->rect().width(),0,ui->widget_side->width(),ui->widget_side->height()));
        m_propertyAnimation->setEndValue(QRect(0,0,ui->widget_side->width(),ui->widget_side->height()));
        m_propertyAnimation->start();
        m_propertyAnimation2->setStartValue(QRect(-1,ui->widget_side->height()/2-ui->pushButton->height()/2,ui->pushButton->width(),ui->pushButton-> height()));
        m_propertyAnimation2->setEndValue(QRect(ui->widget_side->width()-2,ui->widget_side->height()/2-ui->pushButton->height()/2,ui->pushButton->width (),ui->pushButton->height()));
        m_propertyAnimation2->start();
        ui->pushButton->setText("<<");
        m_bSideflag = !m_bSideflag;
    }
    else
    {
        m_propertyAnimation->setStartValue(QRect(0,0,ui->widget_side->width(),ui->widget_side->height()));
        m_propertyAnimation->setEndValue(QRect(-this->rect().width(),0,ui->widget_side->width(),ui->widget_side->height()));
        m_propertyAnimation->start();
        m_propertyAnimation2->setStartValue(QRect(ui->widget_side->width()-2,ui->widget_side->height()/2-ui->pushButton->height()/2,ui->pushButton->width (),ui->pushButton->height()));
        m_propertyAnimation2->setEndValue(QRect(-1,ui->widget_side->height()/2-ui->pushButton->height()/2,ui->pushButton->width(),ui->pushButton-> height()));
        m_propertyAnimation2->start();
        ui->pushButton->setText(">>");
        m_bSideflag = !m_bSideflag;
    }
}

The function of animation effect in Qt is very powerful. For the first time, there are still many functions that need to be explored constantly. Come on!

Optimization ideas:

Add a new qwidget container, and place the menu bar and button controls in the qwidget container to achieve the consistency of the dynamic effects of the two. The following is the final rendering of the implementation:

The article is transferred from Blog Garden (Xiong Lai Chuang Yi Chuang): Qt interface design–sidebar hiding and sliding out – Xiong Lai Chuang Yi Chuang – Blog Garden

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C ++ language foundation, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming , QT project actual combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓