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. Mine field Layout problem (solved).
QtWS25 Last Chance

Mine field Layout problem (solved).

Scheduled Pinned Locked Moved Game Development
7 Posts 2 Posters 2.3k 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.
  • M Offline
    M Offline
    Mangusta89
    wrote on last edited by
    #1

    Hi everyone, I've recently started to develope a simple game (mine field) and I already have a problem.
    I create a QPushButton "newGame", which purpose is to create a grid of buttons.
    I post some lines of the code to help the explanation:

    @
    mine_field::mine_field(QWidget *parent) : QWidget(parent) {

    QHBoxLayout *menuLayout = new QHBoxLayout();
    QPushButton *newGame = new QPushButton(tr("New Game"));
    menuLayout->addWidget(newGame);

    QVBoxLayout *mainLayout = new QVBoxLayout();
    mainLayout->addLayout(menuLayout);
    setLayout(mainLayout);

    connect(newGame, SIGNAL(clicked()), this, SLOT(create()));
    }

    void mine_field::create()
    {
    QGridLayout *buttonLayout = new QGridLayout();
    for (int i=0;i<10;++i)
    for (int j=0; j<10; ++j){
    QPushButton *button = new QPushButton();
    buttonLayout->addWidget(button, i, j);
    }
    mainLayout -> addLayout(buttonLayout);
    }
    @

    I get a segmentation fault, I think because mainLayout is not defined in function create().
    Anyone can help me?
    Thanks.

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

      Hi and welcome to devnet,

      You are shadowing mainLayout in your constructor that's why it crashes in create()

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mangusta89
        wrote on last edited by
        #3

        Thank for the answer, how can I set mainLayout as global, visible both for constructor and for create() ?

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Seems you already do have it as a class member. Otherwise you wouldn't be able to compile

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Mangusta89
            wrote on last edited by
            #5

            Yes, I declared it in header file:
            @ private:
            QHBoxLayout *menuLayout; @

            So instead of write in constructor:
            @ QHBoxLayout *menuLayout = new QHBoxLayout (); @

            Can I use it as?
            @ this->menuLayout @

            1 Reply Last reply
            0
            • M Offline
              M Offline
              Mangusta89
              wrote on last edited by
              #6

              Ok, I resolved the problem just declaring in constructor:
              @menuLayout = new QHBoxLayout ();@

              Thank you for your patience :)

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

                You're welcome !

                Since you have it working now, please update the thread title by prepending [solved] so the other forum users may know a solution has been found :)

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                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