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. [SOLVED] Clear widget layout and repaint with a new one
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Clear widget layout and repaint with a new one

Scheduled Pinned Locked Moved General and Desktop
10 Posts 5 Posters 9.7k 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.
  • H Offline
    H Offline
    holygirl
    wrote on last edited by
    #1

    Hey everybody!

            I have a widget with a set layout at the start of the application. I want to repaint the widget with a new layout on the click of a button. I tried the solutions given "here":http://www.qtforum.org/article/17819/clear-a-widget-and-repaint-with-a-new-layout-solved.html?s=f39f70e41208b92024d876f0438d85222a635871#post69511 and "here":http://stackoverflow.com/questions/4857188/clearing-a-layout-in-qt but it was of no avail. 
    

    Please help! Thanks.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      b1gsnak3
      wrote on last edited by
      #2

      If you don't plan on using the same items in the new layout you can use the QStackedLayout class. Otherwise create a new layout, add your items to the new layout, delete whatever is remaining of the first layouts items, delete the widget->layout() and then call widget->setLayout with the new layout you created.

      1 Reply Last reply
      0
      • H Offline
        H Offline
        holygirl
        wrote on last edited by
        #3

        [quote author="b1gsnak3" date="1360229661"]Otherwise create a new layout, add your items to the new layout[/quote]

        That's what I tried to do but the layout is not getting cleared. Here's my code

        @
        QHBoxLayout *hlayout = new QHBoxLayout();
        QVBoxLayout *vlayout = new QVBoxLayout();

        hlayout->addWidget(label1);
        hlayout->addWidget(label2);

        MainLabel->setLayout(hlayout);

        if(keyEvent->key()==Qt::Key_Down) //If the down arrow key is press
        {
        delete MainLabel->layout();
        vlayout->addWidget(label3);
        vlayout->addWidget(label4);
        MainLabel->setLayout(vlayout);
        }
        @

        1 Reply Last reply
        0
        • B Offline
          B Offline
          b1gsnak3
          wrote on last edited by
          #4

          Well, it's not cleared because you are using different items (label3, 4). If you wanted the same items (label1, 2) then it would've been cleared. However, for this, you should use QStackedLayout and change the index of the layout (0 for your hLayout and 1 for vLayout, for example). If you really want to use this model, then try using:

          @{
          delete MainLabel->layout();
          qDeleteAll(MainLabel->children());
          label3 = new QLabel();
          label4 = new QLabel();
          etc.
          }@

          1 Reply Last reply
          0
          • H Offline
            H Offline
            holygirl
            wrote on last edited by
            #5

            Thanks a lot for your answer, b1gsnak3. qDeleteAll did the trick. Thanks again! :)

            1 Reply Last reply
            0
            • B Offline
              B Offline
              b1gsnak3
              wrote on last edited by
              #6

              your welcome :) mark the topic with solved then :)

              1 Reply Last reply
              0
              • H Offline
                H Offline
                holygirl
                wrote on last edited by
                #7

                Oh yes! I forgot. Sorry.

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  dboddie_work
                  wrote on last edited by
                  #8

                  Since the layout is itself a child of the widget it manages, you should be able to just use qDeleteAll(MainLabel->children()) and drop the delete MainLabel->layout() call.

                  1 Reply Last reply
                  0
                  • I Offline
                    I Offline
                    Igor Gomes
                    wrote on last edited by
                    #9

                    Aqui eu limpei o QFrame para receber novos objetos, meu QFrame Pai se chama "frame_104" e o QFrame filho se chama "frame_grafico", neste exemplo eu preciso limpar o frame filho para atualizar meu grafico que fica dentro dele,sendo assim:
                    QFrame frameGrafico = ui->frame_104->findChild<QFrame>("frame_grafico");

                    // Remover todos os widgets filhos do frameGrafico
                    QList<QWidget*> children = frameGrafico->findChildren<QWidget*>();
                    for (QWidget* child : children) {
                    child->setParent(nullptr); // Desanexar o widget do pai
                    child->deleteLater(); // Marca o widget para ser deletado mais tarde de forma segura
                    }

                    // Remover layout antigo, se existir
                    if (frameGrafico->layout() != nullptr) {
                    QLayout *oldLayout = frameGrafico->layout();
                    frameGrafico->setLayout(nullptr);
                    delete oldLayout;
                    }

                    SGaistS 1 Reply Last reply
                    0
                    • I Igor Gomes

                      Aqui eu limpei o QFrame para receber novos objetos, meu QFrame Pai se chama "frame_104" e o QFrame filho se chama "frame_grafico", neste exemplo eu preciso limpar o frame filho para atualizar meu grafico que fica dentro dele,sendo assim:
                      QFrame frameGrafico = ui->frame_104->findChild<QFrame>("frame_grafico");

                      // Remover todos os widgets filhos do frameGrafico
                      QList<QWidget*> children = frameGrafico->findChildren<QWidget*>();
                      for (QWidget* child : children) {
                      child->setParent(nullptr); // Desanexar o widget do pai
                      child->deleteLater(); // Marca o widget para ser deletado mais tarde de forma segura
                      }

                      // Remover layout antigo, se existir
                      if (frameGrafico->layout() != nullptr) {
                      QLayout *oldLayout = frameGrafico->layout();
                      frameGrafico->setLayout(nullptr);
                      delete oldLayout;
                      }

                      SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @Igor-Gomes hi and welcome to devnet,

                      Thanks for participating but please use English as this is the official language of the forum. There is an international sub-forum where you can use your mother tongue.

                      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