Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Game Development
  4. Game menu
Forum Updated to NodeBB v4.3 + New Features

Game menu

Scheduled Pinned Locked Moved Game Development
2 Posts 1 Posters 2.1k 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.
  • J Offline
    J Offline
    jcxz
    wrote on 27 Nov 2012, 23:09 last edited by
    #1

    Hi all,

    right now I am in process of making a game and currently I am designing my game menu classes.

    I would like to have the menu, like standard flash games have, e.g. when I click on a button in the
    menu widget a new widget replaces it in the same window with another set of buttons representing a submenu, ...

    To do this I was thinking of using QStackedWidget, but I was not able to figure out an elegant way
    to do the back button.

    I came up with something like:
    @static_cast<CMainWindow *>(parent())->setCurrentIndex(0);@
    where this is implemented in a widget added to CMainWindow widget, which is derived from QStackedWidget.
    However this seems to me a kind of hackish.

    Also I came across the QWizard widget, which has a similar behaviour to what I want,
    but I deem that dialog widget is not the right choice for game menu.

    So my question is what do you think would be a good approach to design a menu like ?
    I even tried to go through qt demos and google, but none of them had an example on this kind of thing.

    Thanks in advance for any good advice or links to examples

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jcxz
      wrote on 28 Nov 2012, 11:06 last edited by
      #2

      So i will reply to myself:

      After taking inspiration from "this":http://stackoverflow.com/questions/9651976/transitioning-between-menu-screens-with-qstatemachine SO post and "this":http://pastebin.com/YhbHHJNe code snippet linked in the last comment of the accepted answer, I came up with following solution:

      @class CMenuScreenWidget : public QStackedWidget
      {
      Q_OBJECT

      public:
      explicit CMenuScreenWidget(QWidget *parent = 0)
      : QStackedWidget(parent)
      { }

      protected:
      /**
      * This method will set up a mapping between signals and widgets.
      * This means that when a signal of a sender is triggered,
      * the menuscreen will be switched to make the widget a currently
      * displayed widget.
      *
      * @param sender connect to signals from this object
      * @param signal connect to this particular signal
      * @param widget make this widget current upon triggering a signal
      */
      void setMapping(QObject *sender, const char *signal, QWidget *widget);

      private slots:
      void switchWidget(void);

      private:
      QHash<QObject *, QWidget *> m_mapping; /// to keep mapping between signal senders and widgets
      };@

      @/**
      */
      void CMenuScreenWidget::setMapping(QObject *sender, const char *signal, QWidget *widget)
      {
      addWidget(widget);
      connect(sender, signal, SLOT(switchWidget()));
      m_mapping[sender] = widget;
      return;
      }

      /**
      */
      void CMenuScreenWidget::switchWidget(void)
      {
      setCurrentWidget(m_mapping[sender()]);
      return;
      }
      @

      And usage could be for example like this:

      @class CMainMenuWidget;
      class CSinglePlayerMenuWidget;

      /**
      */
      class CMainWindow : public CMenuScreenWidget
      {
      Q_OBJECT

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

      private:
      CMainMenuWidget *m_main_menu;
      CSinglePlayerMenuWidget *m_single_player;
      };
      @

      @CMainWindow::CMainWindow(QWidget parent)
      : CMenuScreenWidget(parent),
      m_main_menu(0),
      m_single_player(0)
      {
      /
      create widgets */
      m_main_menu = new CMainMenuWidget;
      m_single_player = new CSinglePlayerMenuWidget;

      /* set signals */
      connect(m_main_menu, SIGNAL(exitTriggered()), SLOT(close()));

      /* set up menu mappings and connections between menu screens */
      setMapping(m_main_menu, SIGNAL(singlePlayerTriggered()), m_single_player);
      setMapping(m_single_player, SIGNAL(backTriggered()), m_main_menu);

      /* set current widget */
      setCurrentWidget(m_main_menu);
      }@

      While not the most efficient, I hope this solution is flexible enough for changes in UI,
      which I think is quite an important quality of UI-s in general.

      1 Reply Last reply
      0

      2/2

      28 Nov 2012, 11:06

      • Login

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