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

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

    Ideas:

    • 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 {
        Q_OBJECT
    public:
        Window()
        : menuPanel(new MenuPanel(this)), arenaPanel(new ArenaPanel(this)) {
            setGeometry(100,100,400,300);
            setWindowTitle("Game");
            setCentralWidget(menuPanel);
        }
        ~Window() {
            delete menuPanel;
            delete arenaPanel;
        }
         
    private:
        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 {
        Q_OBJECT
    public:
        MenuPanel(QMainWindow* parent = 0)
        : QWidget(parent), changePanelButton(new QPushButton("Go to ArenaPanel")) {
            
            changePanelButton->setFont(QFont("Helvetica",16, QFont::Normal));
            
            QGridLayout* gridLayout = new QGridLayout();
            gridLayout->addWidget(changePanelButton,0,1);
            setLayout(gridLayout);
        }
        ~MenuPanel() {
            delete changePanelButton;
        }
    private:
        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 :)

    Thanks!

    Leon

    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.


  • Qt Champions 2017

    Hi
    Im not sure if the same as a cardlayout but what about
    http://doc.qt.io/qt-5/qstackedwidget.html
    It provides easy way to flip between "pages" of widgets.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.