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. Problem with QWidgets on GridLayout
Forum Updated to NodeBB v4.3 + New Features

Problem with QWidgets on GridLayout

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 1.8k 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.
  • T Offline
    T Offline
    tianstar
    wrote on last edited by tianstar
    #1

    Hey Guys,

    I have a problem with my Gridlayout. I am using my Gridlayout to manage 2 Gameboards on my GUI. I want the User to be able to change between to Games. Both GameWidgets are already developed.

    My idea was just to start one of the two games in my Constructor with:

    ui->gameBoard->addWidget(&gm,0,0)
    

    and in my Header:

    GameWidget  gm;
    

    Btw my second GameWidget is

    SnakeWidget sm;
    

    So I used a ComboBox to let the User decide what he wants to play:..

    gameIndex = ui->gameCombo->currentIndex();
        switch(gameIndex){
        case 0:ui->gameBoard->addWidget(&gm);break;
        case 1:ui->gameBoard->addWidget(&sm);break;
        }
    

    So if I start my GUI I am not getting any Error´s I even can change my first Game to my second one. But I am not able to change the Games back. I guess it has something to do with that I dont delete the game or so..

    I have really no idea what to do :D. I am still very new in Qt and C++. Maybe I should not use a GridLayout for that. Another important thing is that I have to paint on my GameWidgets or to be able to grab keyboard events or mousepressevents/click.. .

    I hope you can understand my problem so far.. if not I can write anything more specific.
    Sorry for my bad english :).

    I also tried to remove Widgets to replace Widgets but I failed ..

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

      Hi,

      Wouldn't a QStackedWidget be better suited for that task ?

      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
      3
      • T Offline
        T Offline
        tianstar
        wrote on last edited by
        #3

        Never read that word before. I will check it out definitly ! Thanks !

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tianstar
          wrote on last edited by tianstar
          #4

          Ok wow, this one works !
          How can a beginner learn to find those structures ?

          I got another Problem now.. I can play both games this works. But now my gui crashes when I close it :/

          Debugger say something is wrong with my SnakeWidget Class is wrong.
          SIGSEGV Segmentation fault.

          Any fast idea before I can go to bed :D?

          I am getting to the point where the debugger tells me my deconstructor is failing ..?

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

            Hi
            its a double deletion

            GameWidget gm; << those are deleted by their containing class.
            SnakeWidget sm;

            << the layout thinks its own them and will also delete
            case 0:ui->gameBoard->addWidget(&gm);break;
            case 1:ui->gameBoard->addWidget(&sm);break;

            In Qt, objects can own each other and a parent will delete it childs.

            http://doc.qt.io/qt-5/objecttrees.html

            1 Reply Last reply
            2
            • T Offline
              T Offline
              tianstar
              wrote on last edited by tianstar
              #6

              Okay so I need to stop one deletion, I tried now to remove the QStackedWidgets manually in my MainWindow Destructur. As I thought it does not work.

              Is it the problem that I never said, that GameWidget and SnakeWidget should be a Child of my MainWindow ?

              jsulmJ 1 Reply Last reply
              0
              • T tianstar

                Okay so I need to stop one deletion, I tried now to remove the QStackedWidgets manually in my MainWindow Destructur. As I thought it does not work.

                Is it the problem that I never said, that GameWidget and SnakeWidget should be a Child of my MainWindow ?

                jsulmJ Online
                jsulmJ Online
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @tianstar Just allocate the widgets on the stack, and do NOT delete them manually:

                GameWidget  *gm;
                SnakeWidget *sm;
                
                gm = new GameWidget();
                sm = new SnakeWidget();
                
                gameIndex = ui->gameCombo->currentIndex();
                switch(gameIndex){
                    case 0:ui->gameBoard->addWidget(gm);break;
                    case 1:ui->gameBoard->addWidget(sm);break;
                }
                

                They will be automatically deleted when gameBoard is deleted.

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

                1 Reply Last reply
                3
                • T Offline
                  T Offline
                  tianstar
                  wrote on last edited by
                  #8

                  Thank you guys for your help ! really really really great ! :)
                  Have a nice day !

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

                    One small note, you're "doing it wrong"

                    In your constructor:

                    gm = new GameWidget();
                    ui->gameBoard->addWidget(gm)
                    sm = new SnakeWidget();
                    ui->gameBoard->addWidget(sm)
                    
                    connect(ui->gameCombo, qOverload<int>(QComboBox::activated),
                                ui->gameBoard, QStackedWidget::setCurrentIndex);
                    

                    There's no need to add the widgets again and again to the QStackedWidget.

                    If you have errors for qOverload see the corresponding documentation for its alternatives.

                    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
                    2

                    • Login

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