Important: Please read the Qt Code of Conduct -

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

  • 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

  • This post is deleted!

  • Lifetime Qt Champion

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

  • 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: - That is the main class, which loads the game class, but will load the mainmenu class when it is finally working - that is the game class, where the game is initialized, the other classes are loaded and everything starts

  • Lifetime Qt Champion

    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?

  • 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. - 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!

  • Lifetime Qt Champion

    @nanoandrew4 said:

    Maybe I missed it but where is game.h ?

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

  • Here it is -
    Also, mainmenu.h just in case -

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

  • Lifetime Qt Champion

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

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

  • Lifetime Qt Champion

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

  • 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

  • Lifetime Qt Champion


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

    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.

  • @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!

  • Lifetime Qt Champion

    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.

    good luck

Log in to reply