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. QPushButton reposition, Main Menu load game on same scene, not on different windows

QPushButton reposition, Main Menu load game on same scene, not on different windows

Scheduled Pinned Locked Moved General and Desktop
15 Posts 2 Posters 4.2k Views 2 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.
  • N Offline
    N Offline
    nanoandrew4
    wrote on last edited by
    #1

    My first question is about repositioning the QPushButton on the scene, because when I use setGeometry there is always one button in the upper left corner, instead of where it should be, under the other buttons. My second question is about the scene, I'm making a game and I want the scene from the main menu to be reused for the game, instead of opening a new window for it. Could anyone give me a hint on how to achieve that?

    Thank you in advance

    1 Reply Last reply
    0
    • N Offline
      N Offline
      nanoandrew4
      wrote on last edited by nanoandrew4
      #2
      This post is deleted!
      1 Reply Last reply
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi
        Are you talking about an example or where would I see
        "the scene from the main menu " ?

        I think some more info is needed for anyone to help :)

        1 Reply Last reply
        0
        • N Offline
          N Offline
          nanoandrew4
          wrote on last edited by
          #4

          Basically I created a scene for the main menu to be on, and would like that when I hit the "Play" button, that the menu was deleted and the game was started on that scene, instead of making a new one. For the code, here it is:
          http://pastebin.com/qWaaL1Kq - That is the main class, which loads the game class, but will load the mainmenu class when it is finally working

          http://pastebin.com/2y3fEakH - that is the game class, where the game is initialized, the other classes are loaded and everything starts

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

            @nanoandrew4
            Hi
            by Main menu, you mean the mainwindow and not menu like in popup menu?

            So in Game class ( which is a window ?)
            you create a new QGraphicsScene()
            and you want this QGraphicsScene, to be kept and put on a new window that is created/shown
            when you press play?

            1 Reply Last reply
            0
            • N Offline
              N Offline
              nanoandrew4
              wrote on last edited by nanoandrew4
              #6

              No, my bad for my explaining it in a confusing way. The mainmenu class (which I forgot to include, my bad) creates the scene, and I want the scene from the game class to go on the scene from the mainmenu, when I hit "Play" since when a new scene is created, a new window pops up, and that is what I don't want.

              http://pastebin.com/ZVhDqchy - mainmenu class (also, how can I position the buttons so that they are one on top of each other, the code I provided makes it so that one is in the top left corner, I imagine due to their positioning on the scene due to the setGeometry)

              Thank you for your help!

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

                @nanoandrew4 said:

                Maybe I missed it but where is game.h ?

                Not sure what is a window and what is a widget :)

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  nanoandrew4
                  wrote on last edited by nanoandrew4
                  #8

                  Here it is - http://pastebin.com/WuajT37w
                  Also, mainmenu.h just in case - http://pastebin.com/bpkVM3q9

                  By window I just mean that a new window will pop up when a new scene is created. In Qt terms, I think it might be a widget as you said. I'm sorry, I started with Qt recently and terminology isn't my forte, as you can see...

                  mrjjM 1 Reply Last reply
                  0
                  • N nanoandrew4

                    Here it is - http://pastebin.com/WuajT37w
                    Also, mainmenu.h just in case - http://pastebin.com/bpkVM3q9

                    By window I just mean that a new window will pop up when a new scene is created. In Qt terms, I think it might be a widget as you said. I'm sorry, I started with Qt recently and terminology isn't my forte, as you can see...

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @nanoandrew4
                    Hi both mainmenu and Game are QGraphicsView and when you
                    create them with no parents, they become windows.

                    So you could restructure your code so the scene only live in one of the classes and then call
                    setScene(scene) in the other and not
                    scene = new QGraphicsScene(); in both places.

                    You create a new scene in both places.
                    both in
                    MainMenu::MainMenu()
                    Game::Game()
                    So maybe MainMenu will create scene and game just use the same or reverse.
                    (game seems to add extra to scene)

                    So this is how I understand it of what you trying to do:
                    When programs starts, it setup a scene and show it.
                    When user press Play,
                    a new windows is shown and the game starts?
                    The scene in mainmenu is used for Game also.

                    1 Reply Last reply
                    0
                    • N Offline
                      N Offline
                      nanoandrew4
                      wrote on last edited by
                      #10

                      Thank you for the help. You got everything right except that I want it all in one window, not in separate ones. But I will make the mainmenu scene the parent one and reuse it in game. Do I just make the mainmenu a QWidget and a parent pointer, and then just call the pointer from game to set the scene? Also, you are correct, Game adds all the objects for the game to be played.

                      mrjjM 1 Reply Last reply
                      0
                      • N nanoandrew4

                        Thank you for the help. You got everything right except that I want it all in one window, not in separate ones. But I will make the mainmenu scene the parent one and reuse it in game. Do I just make the mainmenu a QWidget and a parent pointer, and then just call the pointer from game to set the scene? Also, you are correct, Game adds all the objects for the game to be played.

                        mrjjM Offline
                        mrjjM Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on last edited by mrjj
                        #11

                        @nanoandrew4
                        Ok. the windows comes from your mainmenu/Game constructor.
                        you made a default one so it has no Parent parameter and it becomes a window.

                        class MainMenu : public QGraphicsView
                        Q_OBJECT
                        public:
                        MainMenu(); <---- this constructor do not take a widget pointer (as parent)
                        so it should be
                        MainMenu(QWidget *parent = 0);

                        and in the implementation
                        MainMenu::MainMenu( QWidget *parent ) : QGraphicsView(parent) ...

                        so if you make a new mainwindow and create mainmenu like
                        MainMenu mwin(mainwindow);
                        it will be a control on that mainwindow and not a window on its own.

                        1 Reply Last reply
                        0
                        • N Offline
                          N Offline
                          nanoandrew4
                          wrote on last edited by
                          #12

                          Ok thank you very much, I will try that now. The other question was about how to position the buttons one on top of another, since when I use setGeometry, one ends up in the top ledt corner instead of below the others. The code is in mainmenu.cpp

                          mrjjM 1 Reply Last reply
                          0
                          • N nanoandrew4

                            Ok thank you very much, I will try that now. The other question was about how to position the buttons one on top of another, since when I use setGeometry, one ends up in the top ledt corner instead of below the others. The code is in mainmenu.cpp

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @nanoandrew4

                            Hi
                            here is a small sample using QGraphicsView as a control on a mainwindow not as a window by it self.
                            Maybe it says more than many words :)
                            https://www.dropbox.com/s/260uga0gma6j9lc/nanotest2.zip?dl=0

                            how to position the buttons one on top of another

                            playButton->setGeometry(screenWidth /2, 100, 100,50);

                            if they go in top left corner, i think maybe screenWidth is zero here ?
                            Using setGeometry should be fine so check the values are what you expect.

                            N 1 Reply Last reply
                            0
                            • mrjjM mrjj

                              @nanoandrew4

                              Hi
                              here is a small sample using QGraphicsView as a control on a mainwindow not as a window by it self.
                              Maybe it says more than many words :)
                              https://www.dropbox.com/s/260uga0gma6j9lc/nanotest2.zip?dl=0

                              how to position the buttons one on top of another

                              playButton->setGeometry(screenWidth /2, 100, 100,50);

                              if they go in top left corner, i think maybe screenWidth is zero here ?
                              Using setGeometry should be fine so check the values are what you expect.

                              N Offline
                              N Offline
                              nanoandrew4
                              wrote on last edited by
                              #14

                              @mrjj Thank you for the example, it was helpful, but what I want is a window with the play button on it, and when it is pressed, for the button to be deleted and the game started. Its kind of hard to explain, so I will try to solve it myself, but basically I want a scene that leads to another scene within the same window, not a button on the scene where the game runs. I'm sorry for the lack of good communication, it is just somewhat hard to explain

                              Thank you for your help!

                              mrjjM 1 Reply Last reply
                              0
                              • N nanoandrew4

                                @mrjj Thank you for the example, it was helpful, but what I want is a window with the play button on it, and when it is pressed, for the button to be deleted and the game started. Its kind of hard to explain, so I will try to solve it myself, but basically I want a scene that leads to another scene within the same window, not a button on the scene where the game runs. I'm sorry for the lack of good communication, it is just somewhat hard to explain

                                Thank you for your help!

                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by mrjj
                                #15

                                @nanoandrew4
                                Hi
                                Some stuff are just hard to explain :)
                                So this button, is just a normal button, not one drawn inside scene ?
                                So you see the scene / a scene / in the background and there is a button over it / in center and
                                when you press Play, it vanish and the scene changes into the actual scene for the game?
                                Like in a good old arcade game ?

                                You can hide the button with hide()

                                here is sample which has button over scene1, when pressed it vanish and the scene is change to scene 2.

                                https://www.dropbox.com/s/zbh712v02m7obm6/nanotest3.zip?dl=0

                                good luck

                                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