Important: Please read the Qt Code of Conduct -

QPropertyAnimation for sliding menu [Solved]

  • Hello all,

    I am relatively new to Qt and C++ and I'm currently stuck trying to implement a sliding menu. The menu slides out from the side when the m_button is pressed:

    I use this to make my the window and the menu
    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)

    m_button = new QPushButton("My Button", this);
    m_button->setGeometry(QRect(QPoint(100, 100),
                                 QSize(200, 50)));
    //Menu layout and widget
    QWidget *menu = new QWidget(this);
    QVBoxLayout *hlayout = new QVBoxLayout;
    //Create 4 labels to populate the menu
    for(int i =0; i<4; i++){
        label[i] = new QLabel("Menu");
    //show the menu
    // Connect button signal to slot
    connect(m_button, SIGNAL(released()), this, SLOT(handleButton()));


    Ideally this would move the menu into the correct location
    void MainWindow::handleButton()
    //Create animation
    QPropertyAnimation *animation = new QPropertyAnimation(menu, "geometry");
    QRect startRect(-100,50,50,200);
    QRect endRect(50,50,50,200);

    However it only crashes the program. The code works if the menu widget is created when the button is pressed but I don't want a new instance of the menu every time I press the button. I have a feeling it is something obvious but can't figure it out.


  • Hi,

    Welcome to Devnet,

    In your code Line 10: you have declared "menu" inside the constructor, so the scope of "menu" is just inside the constructor body, You need to declare it in the header file(mainwindow.h).

    Check if this works...

  • Thanks for your feedback,

    Currently my header looks like this
    namespace Ui {
    class MainWindow;

    class MainWindow : public QMainWindow

    explicit MainWindow(QWidget *parent = 0);

    private slots:
    void handleButton();

    QPushButton *m_button;
    QPropertyAnimation *animation;
    QWidget menu;


    Do I need to initialize the 'QWidget *menu;' differently?

  • If you have already declared in your header file then you just need to change Line 10 in you constructor to

    @menu = new QWidget(this);@

    Since you are using
    @QWidget *menu = new QWidget(this);@ inside the constructor , it creates a new reference object (menu) of type QWidget whose socpe of access is just inside the constructor, but the one you are using in MainWindow::handleButton() function is still null so the program crashes.

  • You should change line 10
    QWidget *menu = new QWidget(this);

    menu = new QWidget(this);

    because in first variant you declare and define new variable with name "menu", but "menu" member of class is still uninitialized.

  • Thanks for that! Worked like a charm

  • [quote author="callum.chalmers" date="1355813045"]Thanks for that! Worked like a charm[/quote]

    Glad to be of some help, Kindly Edit your first post and prepend [Solved] to the title.

Log in to reply