Не работает QPropertyAnimation

Рейтинг: 1Ответов: 1Опубликовано: 09.04.2023

У меня есть код:

testbutton.h

#include "QMainWindow"
#include "QPushButton"
#include "QHBoxLayout"
#include "QFrame"
#include "QSize"


class testbutton : public QMainWindow
{
    Q_OBJECT
private:

public:
    testbutton();

    QFrame *m_button_bg;
    QPushButton *text_button;
    QPushButton *icon_button;
    QHBoxLayout *m_buttonLayout;
    QSize *maximumsize;

    void setButtonText(QString text, QFont font);
    bool pressed;
private Q_SLOTS:
    void buttonPressEvent(bool pressed_arg);
};

testbutton.cpp

#include "testbutton.h"
#include "QPropertyAnimation"

#include "QPushButton"
#include "QHBoxLayout"
#include "QFrame"

testbutton::testbutton()
{
    text_button = new QPushButton(this);
    text_button->setText("example");
    text_button->setFixedHeight(39);
    icon_button = new QPushButton(this);
    icon_button->setFixedHeight(39);
    icon_button->setText("X");
    connect(text_button, SIGNAL(clicked(bool)), this, SLOT(buttonPressEvent(bool)));
    connect(icon_button, SIGNAL(clicked(bool)), this, SLOT(buttonPressEvent(bool)));

    m_button_bg = new QFrame(this);
    m_button_bg->setFixedHeight(39);

    m_buttonLayout = new QHBoxLayout(m_button_bg);
    m_buttonLayout->addWidget(text_button);
    m_buttonLayout->addWidget(icon_button);
    bool pressed = false;
}
void testbutton::buttonPressEvent(bool pressed_arg){
    QPropertyAnimation *animation = new QPropertyAnimation(text_button, "geometry", this);
    pressed_arg = pressed;
    animation->setDuration(10000);
    animation->setEasingCurve(QEasingCurve::InCurve);
    if(pressed_arg == true)
        {
            animation->setStartValue(text_button->size());
            animation->setEndValue(text_button->minimumSize());
            pressed = false;


         }
    else{
        animation->setStartValue(text_button->size());
        animation->setEndValue(text_button->maximumSize());
        pressed = true;

    }
    animation->start();
}

main.cpp

#include "testbutton.h"

#include <QApplication>
#include <QPushButton>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    testbutton b;
    b.show();
    return a.exec();
}

От него я ождиаю, что кнопка text_button, при нажатии на неё или на icon_button, будет плавно уменьшать длину справа налево, а icon_button будет вместе с правой стороной text_button сдвигаться налево. А после окончания анимации, при нажатии на icon_button, text_button будет вновь расширяться к прежней длине, сдвигая свой правой стороной icon_text вправо. Но от данного кода я не получаю никакой анимации, при нажатии на любую из кнопок, кнопка с текстом(text_button) просто исчезает, без любой анимации, а icon_button не сдвигается с места. Прошу указать на мою ошибку, хотя бы, почему не происходит никакой анимации.

Ответы

▲ 0Принят

Если зайти в документацию QWidget вы можете найти там список Properties. Там же указаны соответствующие типы. Свойству geometry соответствует QRect, а не QSize. Либо используйте свойство size, либо подавайте QRect, но и тот и другой варианты не сочетаются с вашим QHBoxLayout, который сам определяет необходимые размеры. Для более корректного поведения вам скорее всего придётся поиграться со свойствами maximumWidth/maximumSize/minimumSize и т.п.