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. Dynamic menu in Qt
QtWS25 Last Chance

Dynamic menu in Qt

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 5 Posters 797 Views
  • 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.
  • TequiloutreT Offline
    TequiloutreT Offline
    Tequiloutre
    wrote on last edited by
    #1

    Hello,

    I'm French so sorry if my english is not perfect :)

    I'm pretty knew on Qt and I'm stuck with something that I think is supposed to be simple but maybe it is impossible in Qt.
    I'm recreating the fight system of Pokemon games and after doing it on console, I'm trying to adapt it on Qt to have a graphical interface.

    For now, I have my main window with a QGridLayout "hud" that contain a QGridLayout on the top right of the screen with the opponent's pokemon infos, another QGridLayout on the bottom left of the screen with the player's pokemon infos and a QVBoxLayout "menu" on the bottom right of the screen with 4 QPushButtons :

    • Attack
    • Team
    • Bag
    • Run

    What I want to do know is when we click on the "Attack" button, the menu change and now show the pokemon's capacity's names on the 4 QPushButtons instead of the 4 1st menu buttons.

    Thanks for your help and time :)
    Tequi.

    jsulmJ 1 Reply Last reply
    0
    • TequiloutreT Tequiloutre

      Hello,

      I'm French so sorry if my english is not perfect :)

      I'm pretty knew on Qt and I'm stuck with something that I think is supposed to be simple but maybe it is impossible in Qt.
      I'm recreating the fight system of Pokemon games and after doing it on console, I'm trying to adapt it on Qt to have a graphical interface.

      For now, I have my main window with a QGridLayout "hud" that contain a QGridLayout on the top right of the screen with the opponent's pokemon infos, another QGridLayout on the bottom left of the screen with the player's pokemon infos and a QVBoxLayout "menu" on the bottom right of the screen with 4 QPushButtons :

      • Attack
      • Team
      • Bag
      • Run

      What I want to do know is when we click on the "Attack" button, the menu change and now show the pokemon's capacity's names on the 4 QPushButtons instead of the 4 1st menu buttons.

      Thanks for your help and time :)
      Tequi.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Tequiloutre You can either reuse the existing buttons (change their text, and store the current state in a variable, so you know what to do if a button is pressed). Or you design distinct widgets for both states. You can use https://doc.qt.io/qt-5/qstackedwidget.html to easily switch between both states.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • TequiloutreT Offline
        TequiloutreT Offline
        Tequiloutre
        wrote on last edited by
        #3

        Hello @jsulm

        Thanks for your reply.

        I've tried QStackedWidget that seems right for this case, but it doesn't work, I've certainly done something wrong :/

        I created two QPushButton for the two possibilities :

        QPushButton *m_button_attack = new QPushButton("Attaquer");
        QPushButton *m_button_capacity_1 = new QPushButton("Capacité 1");
        

        Then I created the QStackedWidget with those QPushButton :

        QStackedWidget *m_button_1 = new QStackedWidget;
        m_button_1->addWidget(m_button_attack);
        m_button_1->addWidget(m_button_capacity_1);
        

        And I connected this QStackedWidget to change index on click (maybe I've made a mistake here ?)

        QObject::connect(m_button_attack, SIGNAL(clicked()), m_button_1, SLOT(setCurrentIndex(1)));
        

        And this QStackedWidget is inside some layouts to be showned in the right place.
        The problem is that the QStackedWidget shows but do nothing on click and I have this error :

        QObject::connect: No such slot QStackedWidget::setCurrentIndex(1) in ../Pokemon/Window.cpp:111
        
        jsulmJ 1 Reply Last reply
        0
        • TequiloutreT Tequiloutre

          Hello @jsulm

          Thanks for your reply.

          I've tried QStackedWidget that seems right for this case, but it doesn't work, I've certainly done something wrong :/

          I created two QPushButton for the two possibilities :

          QPushButton *m_button_attack = new QPushButton("Attaquer");
          QPushButton *m_button_capacity_1 = new QPushButton("Capacité 1");
          

          Then I created the QStackedWidget with those QPushButton :

          QStackedWidget *m_button_1 = new QStackedWidget;
          m_button_1->addWidget(m_button_attack);
          m_button_1->addWidget(m_button_capacity_1);
          

          And I connected this QStackedWidget to change index on click (maybe I've made a mistake here ?)

          QObject::connect(m_button_attack, SIGNAL(clicked()), m_button_1, SLOT(setCurrentIndex(1)));
          

          And this QStackedWidget is inside some layouts to be showned in the right place.
          The problem is that the QStackedWidget shows but do nothing on click and I have this error :

          QObject::connect: No such slot QStackedWidget::setCurrentIndex(1) in ../Pokemon/Window.cpp:111
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by jsulm
          #4

          @Tequiloutre said in Dynamic menu in Qt:

          QObject::connect(m_button_attack, SIGNAL(clicked()), m_button_1, SLOT(setCurrentIndex(1)));

          This is not how signals/slots work - you can't pass parameters like this while connecting signals with slots.
          Use new connect syntax and lambdas:

          connect(m_button_attack, &QPushButton::clicked, [this](){ m_button_1->setCurrentIndex(1); });
          

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          TequiloutreT 1 Reply Last reply
          2
          • jsulmJ jsulm

            @Tequiloutre said in Dynamic menu in Qt:

            QObject::connect(m_button_attack, SIGNAL(clicked()), m_button_1, SLOT(setCurrentIndex(1)));

            This is not how signals/slots work - you can't pass parameters like this while connecting signals with slots.
            Use new connect syntax and lambdas:

            connect(m_button_attack, &QPushButton::clicked, [this](){ m_button_1->setCurrentIndex(1); });
            
            TequiloutreT Offline
            TequiloutreT Offline
            Tequiloutre
            wrote on last edited by
            #5

            @jsulm

            I tried your solution but I have an error about the connect syntax you gave me :

            erreur : variable 'm_button_1' cannot be implicitly captured in a lambda with no capture-default specified
            
            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Then also capture m_button_1 ... https://en.cppreference.com/w/cpp/language/lambda

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • TequiloutreT Offline
                TequiloutreT Offline
                Tequiloutre
                wrote on last edited by
                #7

                I don't completely understand but I've made some research and the connect seems to work when declared like this :

                QObject::connect(m_button_attack, &QPushButton::clicked, m_button_1, [=]() { m_button_1->setCurrentIndex(1); });
                

                Thanks you all for your help :)
                Have a good and safe day :D

                JKSHJ 1 Reply Last reply
                1
                • TequiloutreT Tequiloutre

                  I don't completely understand but I've made some research and the connect seems to work when declared like this :

                  QObject::connect(m_button_attack, &QPushButton::clicked, m_button_1, [=]() { m_button_1->setCurrentIndex(1); });
                  

                  Thanks you all for your help :)
                  Have a good and safe day :D

                  JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on last edited by
                  #8

                  @Tequiloutre said in Dynamic menu in Qt:

                  I don't completely understand but I've made some research and the connect seems to work when declared like this :

                  When you write [=] in your lambda, it means "capture all variables by-value". So, you can use all variables inside your lambda.

                  When you write [this] in your lambda, it means "capture this by-value". So, you can use this inside your lambda, but you cannot use other external variables.

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  JonBJ 1 Reply Last reply
                  3
                  • JKSHJ JKSH

                    @Tequiloutre said in Dynamic menu in Qt:

                    I don't completely understand but I've made some research and the connect seems to work when declared like this :

                    When you write [=] in your lambda, it means "capture all variables by-value". So, you can use all variables inside your lambda.

                    When you write [this] in your lambda, it means "capture this by-value". So, you can use this inside your lambda, but you cannot use other external variables.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #9
                    This post is deleted!
                    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