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. Replacing widget pointer in layout

Replacing widget pointer in layout

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 2 Posters 955 Views 1 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.
  • S Offline
    S Offline
    sandro4912
    wrote on last edited by
    #1

    I have a Widget which i created in the constructor and placed into a layout.

    Game::Game(QWidget *parent) :
        QWidget(parent),
        //...
        mMinefield{ new Minefield},
        //...
    {
        auto topLayout = new QHBoxLayout;
        //.. other widgets get added here
        mTopFrame->setLayout(topLayout);
    
        auto bottomLayout = new QHBoxLayout;
        bottomLayout->addWidget(mMinefield);
        mBottomFrame->setLayout(bottomLayout);
    
        auto mainLayout = new QVBoxLayout;
        mainLayout->addWidget(mTopFrame);
        mainLayout->addWidget(mBottomFrame);
    
        mMainFrame->setLayout(mainLayout);
    
        auto layout = new QVBoxLayout;
        layout->addWidget(mMainFrame);
    
        setLayout(layout);
    }
    

    Now at some point i want to delete this Minefield and replace it with a new one:

    I tryed this:

    void Game::makeNewMinefield()
    {
        delete mMinefield;
        mMinefield = new Minefield;
    }
    

    The result is the old widget is gone but i don't see a new one. What is the correct way here to replace the widget?

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

      Hi,

      Set your widget in the layout.

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

      S 1 Reply Last reply
      2
      • SGaistS SGaist

        Hi,

        Set your widget in the layout.

        S Offline
        S Offline
        sandro4912
        wrote on last edited by
        #3

        @SGaist

        What do you mean with that?

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

          For example use replaceWidget.

          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
          • S Offline
            S Offline
            sandro4912
            wrote on last edited by
            #5

            I tryed this:

            void Game::makeNewMinefield()
            {
                auto newMinefield = new Minefield;
                auto returnItem = layout()->replaceWidget(mMinefield, newMinefield);
                if(returnItem) {
                    qDebug() << "success";
                }
                else {
                    qDebug() << "fail";
                }
                mMinefield = newMinefield;
            }
            

            but i get fail. It seems like he cant find the old widget event if its already placed.

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

              Silly question but are you sure you have that widget set in the first place ?

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

              S 1 Reply Last reply
              1
              • SGaistS SGaist

                Silly question but are you sure you have that widget set in the first place ?

                S Offline
                S Offline
                sandro4912
                wrote on last edited by
                #7

                @SGaist

                I have an idea why its not working.

                I put it in a layout in the constructor:

                bottomLayout->addWidget(mMinefield);
                

                Then it gets into a QFrame:

                mBottomFrame->setLayout(bottomLayout);
                

                Then the two frames i have get played into annother layout

                mainLayout->addWidget(mTopFrame);
                mainLayout->addWidget(mBottomFrame);
                

                This Layout gets played into annotther frame:

                mMainFrame->setLayout(mainLayout);
                

                this frame gets played in annother layout which is the widgets layout:

                layout->addWidget(mMainFrame);
                    setLayout(layout);
                

                So i guess i have to look for the layout in the frame?

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  sandro4912
                  wrote on last edited by
                  #8

                  I could replace it like this:

                  void Game::makeNewMinefield()
                  {
                      auto newMinefield = new Minefield{
                          mDefaultFieldWidth,
                          mDefaultFieldHeight,
                          mDefaultMines
                      };
                      auto returnItem = mBottomFrame->layout()->replaceWidget(
                                  mMinefield, newMinefield);
                      if(returnItem) {
                          qDebug() << "success";
                      }
                      else {
                          qDebug() << "fail";
                      }
                      mMinefield = newMinefield;
                  }
                  

                  It seems to work.
                  Is this the correct way then? What should i do with returnItem in real code?

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

                    Delete it since you won't use its content.

                    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
                    • S Offline
                      S Offline
                      sandro4912
                      wrote on last edited by
                      #10

                      Ok I think the issue is solved. Again something learned.

                      void Game::makeNewMinefield()
                      {
                          auto newMinefield = new Minefield{
                              mDefaultFieldWidth,
                              mDefaultFieldHeight,
                              mDefaultMines
                          };
                          auto returnItem = mBottomFrame->layout()->replaceWidget(
                                      mMinefield, newMinefield);
                      
                          if(!returnItem) {
                              QMessageBox::critical(this, tr("Internal Error"),
                                                             tr("Minefield could not be replaced"),
                                                             QMessageBox::Close);
                              qApp->quit();
                          }
                          delete returnItem;
                          mMinefield = newMinefield;
                      }
                      
                      1 Reply Last reply
                      1

                      • Login

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