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. How to change the visible Panel?
Forum Updated to NodeBB v4.3 + New Features

How to change the visible Panel?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 740 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.
  • L Offline
    L Offline
    Leon_2001
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      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.

      1 Reply Last reply
      3

      • Login

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