Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Frame Size Extention Animation
Qt 6.11 is out! See what's new in the release blog

Frame Size Extention Animation

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 908 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    IknowQT
    wrote on last edited by
    #1

    I want to change the side frame size variably using the toggle button, and I implemented it as a set Fixed Size, but when the size changes, I want to give an animation effect.

    What can I do? I'll attach a rough code.

    ########################### Header ###########################

    #pragma once
    
    #include <QWidget>
    #include "ui_WToggleMenu.h"
    
    #include <qpropertyanimation.h>
    #include <qpainter.h>
    #include <qstyleoption.h>
    
    class WToggleMenu : public QWidget
    {
    	Q_OBJECT
    		Q_PROPERTY(int ExtendPage READ ExtendPage WRITE SetExtendPage)
    
    
    public:
    	WToggleMenu(QWidget *parent = Q_NULLPTR);
    	~WToggleMenu();
    
    private:
    	Ui::WToggleMenu		ui;
    	int					m_nWidth;
    	int					m_nOriWidth;
    	QPropertyAnimation* m_pAniaction;
    
    public:
    	void	ToggleLayout();
    	int		ExtendPage();
    	void	SetExtendPage(int nExtend);
    
    public slots:
    	void BtnToggle();
    
    protected:
    	//void paintEvent(QPaintEvent*) override;
    
    };
    

    ########################### CPP ############################

    #include "WToggleMenu.h"
    
    WToggleMenu::WToggleMenu(QWidget *parent)
    	: QWidget(parent)
    {
    	ui.setupUi(this);
    
    	this->connect(this->ui.btn_toggle, &QPushButton::clicked, this, &WToggleMenu::BtnToggle);
    	m_nOriWidth = this->ui.frame_left_menu->width();
    	m_nWidth = m_nOriWidth;
    
    	ToggleLayout();
    }
    
    WToggleMenu::~WToggleMenu()
    {
    }
    
    void WToggleMenu::ToggleLayout()
    {
    	m_pAniaction = new QPropertyAnimation(this, "ExtendPage", this);
    	m_pAniaction->setEasingCurve(QEasingCurve::OutBounce);
    	m_pAniaction->setDuration(500);
    
    	SetExtendPage(300);
    }
    
    int WToggleMenu::ExtendPage()
    {
    	return m_nWidth;
    }
    
    void WToggleMenu::SetExtendPage(int nExt)
    {
    	m_nWidth = nExt;
    	this->update();
    }
    
    //void WToggleMenu::paintEvent(QPaintEvent*)
    //{
    //	QPainter painter(this);
    //	QStyleOption opt;
    //	opt.initFrom(this);
    //	style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
    //}
    
    void WToggleMenu::BtnToggle()
    {
    	m_pAniaction->stop();
    
    	if (this->ui.btn_toggle->isChecked())
    	{
    		this->ui.frame_left_menu->setFixedWidth(m_nWidth);
    		m_pAniaction->setEndValue(m_nWidth);
    	}
    	else
    	{
    		this->ui.frame_left_menu->setFixedWidth(m_nOriWidth);
    		m_pAniaction->setEndValue(m_nOriWidth);
    	}
    	m_pAniaction->start();
    }
    
    
    jsulmJ 1 Reply Last reply
    0
    • I IknowQT

      I want to change the side frame size variably using the toggle button, and I implemented it as a set Fixed Size, but when the size changes, I want to give an animation effect.

      What can I do? I'll attach a rough code.

      ########################### Header ###########################

      #pragma once
      
      #include <QWidget>
      #include "ui_WToggleMenu.h"
      
      #include <qpropertyanimation.h>
      #include <qpainter.h>
      #include <qstyleoption.h>
      
      class WToggleMenu : public QWidget
      {
      	Q_OBJECT
      		Q_PROPERTY(int ExtendPage READ ExtendPage WRITE SetExtendPage)
      
      
      public:
      	WToggleMenu(QWidget *parent = Q_NULLPTR);
      	~WToggleMenu();
      
      private:
      	Ui::WToggleMenu		ui;
      	int					m_nWidth;
      	int					m_nOriWidth;
      	QPropertyAnimation* m_pAniaction;
      
      public:
      	void	ToggleLayout();
      	int		ExtendPage();
      	void	SetExtendPage(int nExtend);
      
      public slots:
      	void BtnToggle();
      
      protected:
      	//void paintEvent(QPaintEvent*) override;
      
      };
      

      ########################### CPP ############################

      #include "WToggleMenu.h"
      
      WToggleMenu::WToggleMenu(QWidget *parent)
      	: QWidget(parent)
      {
      	ui.setupUi(this);
      
      	this->connect(this->ui.btn_toggle, &QPushButton::clicked, this, &WToggleMenu::BtnToggle);
      	m_nOriWidth = this->ui.frame_left_menu->width();
      	m_nWidth = m_nOriWidth;
      
      	ToggleLayout();
      }
      
      WToggleMenu::~WToggleMenu()
      {
      }
      
      void WToggleMenu::ToggleLayout()
      {
      	m_pAniaction = new QPropertyAnimation(this, "ExtendPage", this);
      	m_pAniaction->setEasingCurve(QEasingCurve::OutBounce);
      	m_pAniaction->setDuration(500);
      
      	SetExtendPage(300);
      }
      
      int WToggleMenu::ExtendPage()
      {
      	return m_nWidth;
      }
      
      void WToggleMenu::SetExtendPage(int nExt)
      {
      	m_nWidth = nExt;
      	this->update();
      }
      
      //void WToggleMenu::paintEvent(QPaintEvent*)
      //{
      //	QPainter painter(this);
      //	QStyleOption opt;
      //	opt.initFrom(this);
      //	style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);
      //}
      
      void WToggleMenu::BtnToggle()
      {
      	m_pAniaction->stop();
      
      	if (this->ui.btn_toggle->isChecked())
      	{
      		this->ui.frame_left_menu->setFixedWidth(m_nWidth);
      		m_pAniaction->setEndValue(m_nWidth);
      	}
      	else
      	{
      		this->ui.frame_left_menu->setFixedWidth(m_nOriWidth);
      		m_pAniaction->setEndValue(m_nOriWidth);
      	}
      	m_pAniaction->start();
      }
      
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @IknowQT said in Frame Size Extention Animation:

      What can I do?

      You can read documentation: https://doc.qt.io/qt-5/animation-overview.html
      You did not specify start and end value for the animation.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      I 1 Reply Last reply
      1
      • jsulmJ jsulm

        @IknowQT said in Frame Size Extention Animation:

        What can I do?

        You can read documentation: https://doc.qt.io/qt-5/animation-overview.html
        You did not specify start and end value for the animation.

        I Offline
        I Offline
        IknowQT
        wrote on last edited by IknowQT
        #3

        @jsulm

        void WToggleMenu::BtnToggle()
        {
        	bool bToggle = this->ui.btn_toggle->isChecked();
        	if (bToggle)
        		m_nWidth = 300;
        	else 
        		m_nWidth = 80;
        
        	int nWidth = this->ui.frame_left_menu->width();
        	auto anim = new QPropertyAnimation(this, "minimumWidth", this);
        	anim->setDuration(250);
        	anim->setStartValue(80);
        	anim->setEndValue(300);
        	anim->setDirection(bToggle ? QAbstractAnimation::Forward : QAbstractAnimation::Backward);
        	anim->setEasingCurve(bToggle ? QEasingCurve::OutCubic : QEasingCurve::InCubic);
        	anim->start(QAbstractAnimation::DeleteWhenStopped);
        }
        
        void WToggleMenu::paintEvent(QPaintEvent*)
        {
        	/*QPainter painter(this);
        	QStyleOption opt;
        	opt.initFrom(this);
        	style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);*/
        
        	bool bToggle = this->ui.btn_toggle->isChecked();
        
        	if( bToggle )
        		this->ui.frame_left_menu->setFixedWidth(300);
        	else 
        		this->ui.frame_left_menu->setFixedWidth(80);
        
        }
        

        Even if you run it like this, the animation effect doesn't come out.

        How do you know and express the control that is the subject of the animation effect?

        jsulmJ 1 Reply Last reply
        0
        • I IknowQT

          @jsulm

          void WToggleMenu::BtnToggle()
          {
          	bool bToggle = this->ui.btn_toggle->isChecked();
          	if (bToggle)
          		m_nWidth = 300;
          	else 
          		m_nWidth = 80;
          
          	int nWidth = this->ui.frame_left_menu->width();
          	auto anim = new QPropertyAnimation(this, "minimumWidth", this);
          	anim->setDuration(250);
          	anim->setStartValue(80);
          	anim->setEndValue(300);
          	anim->setDirection(bToggle ? QAbstractAnimation::Forward : QAbstractAnimation::Backward);
          	anim->setEasingCurve(bToggle ? QEasingCurve::OutCubic : QEasingCurve::InCubic);
          	anim->start(QAbstractAnimation::DeleteWhenStopped);
          }
          
          void WToggleMenu::paintEvent(QPaintEvent*)
          {
          	/*QPainter painter(this);
          	QStyleOption opt;
          	opt.initFrom(this);
          	style()->drawPrimitive(QStyle::PE_Widget, &opt, &painter, this);*/
          
          	bool bToggle = this->ui.btn_toggle->isChecked();
          
          	if( bToggle )
          		this->ui.frame_left_menu->setFixedWidth(300);
          	else 
          		this->ui.frame_left_menu->setFixedWidth(80);
          
          }
          

          Even if you run it like this, the animation effect doesn't come out.

          How do you know and express the control that is the subject of the animation effect?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @IknowQT said in Frame Size Extention Animation:

          How do you know and express the control that is the subject of the animation effect?

          Not sure what this means. First parameter in the QPropertyAnimation constructor specifies the widget to animate, second specifies the name of the property of that widget to animate.
          Why do you want to animate minimumWidth? Wouldn't it make more sense to animate https://doc.qt.io/qt-5/qwidget.html#width-prop?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved