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. QPropertyAnimation for sliding menu [Solved]
Forum Updated to NodeBB v4.3 + New Features

QPropertyAnimation for sliding menu [Solved]

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 9.3k Views 1 Watching
  • 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.
  • C Offline
    C Offline
    callum.chalmers
    wrote on last edited by
    #1

    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");
        hlayout->addWidget(label[i]);
    }
    
    //show the menu
    menu->setLayout(hlayout);
    menu->setGeometry(QRect(QPoint(-100,50),QSize(150,150)));
    menu->show();
    
    // 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");
    animation->setDuration(1000);
    QRect startRect(-100,50,50,200);
    QRect endRect(50,50,50,200);
    animation->setStartValue(startRect);//QPoint(btnRight,btnBottom));//QPoint(0,0));
    animation->setEndValue(endRect);//QPoint(100,100))
    animation->start();
    }
    @

    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.

    Thanks

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sam
      wrote on last edited by
      #2

      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...

      1 Reply Last reply
      0
      • C Offline
        C Offline
        callum.chalmers
        wrote on last edited by
        #3

        Thanks for your feedback,

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

        class MainWindow : public QMainWindow
        {
        Q_OBJECT

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

        private slots:
        void handleButton();

        private:
        QPushButton *m_button;
        QPropertyAnimation *animation;
        QWidget menu;
        QLabel
        label[4];

        };
        @

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

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Sam
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • _ Offline
            _ Offline
            _rmn
            wrote on last edited by
            #5

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

            to
            @
            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.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              callum.chalmers
              wrote on last edited by
              #6

              Thanks for that! Worked like a charm

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Sam
                wrote on last edited by
                #7

                [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.

                1 Reply Last reply
                0

                • Login

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