Important: Please read the Qt Code of Conduct -

How to change the visible Panel?

  • Hello,

    it's kind of hard to describe my problem at least in a way Google understands it. I guess it's probably a general question I also wondered about in JavaFX for example.

    What I want:
    An application, which can display too Panels/Layouts, but only one shown at the same time (one can switch between them through a button).

    I have a Class inheriting from QWidget. It should be kind of a Game menu with a few Buttons like "Start Game", "Credits" ...

    I have a Class inheriting from QWidget. It should be the actual stage, where to play on.

    When the application starts, the first widget should be displayed. Through pressing the "Start Game" Button, the second Widget should be displayed. While playing, a shortcut, for example, should display the first widget instead again.

    Important: Everything in one window, so I don't want to just pop up a second window.


    • One Window class inheriting from QMainWindow. It has the method setCentralWidget(), where I can first set the first widget and later through Events the second widget
    class Window : public QMainWindow {
        : menuPanel(new MenuPanel(this)), arenaPanel(new ArenaPanel(this)) {
        ~Window() {
            delete menuPanel;
            delete arenaPanel;
        MenuPanel* menuPanel;
        ArenaPanel* arenaPanel;

    And the MenuPanel:

    #ifndef MENUPANEL_H
    #define MENUPANEL_H
    #include <QWidget>
    #include <QtWidgets/QGridLayout>
    #include <QtWidgets/QPushButton>
    #include <QtWidgets/QMainWindow>
    class MenuPanel : public QWidget {
        MenuPanel(QMainWindow* parent = 0)
        : QWidget(parent), changePanelButton(new QPushButton("Go to ArenaPanel")) {
            changePanelButton->setFont(QFont("Helvetica",16, QFont::Normal));
            QGridLayout* gridLayout = new QGridLayout();
        ~MenuPanel() {
            delete changePanelButton;
        QPushButton* changePanelButton;
    #endif //MENUPANEL_H

    ArenaPanel looks the same except of the fact that the Button has of course another text.

    Problem: One Button is centred as it should (because it's the only button), but the other button, which shouldn't be displayed (only after clicking the centred button) is in the upper left corner (much smaller and not the whole text). So instead of just one widget, both are displayed (in a strange way).

    Any Advice?

    Besides this idea, I wonder if this is the right idea? How would you solve this? Any Ideas or sample codes would be great :)



    By the way, is there any name for this problem. I had no idea how I could describe this problem for a google search. In Java swing it is called cardLayout I think, but I am not sure at all.

  • Lifetime Qt Champion

    Im not sure if the same as a cardlayout but what about
    It provides easy way to flip between "pages" of widgets.

Log in to reply