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] Removing objects from QLayout; adding new ones.
Forum Updated to NodeBB v4.3 + New Features

[Solved] Removing objects from QLayout; adding new ones.

Scheduled Pinned Locked Moved General and Desktop
9 Posts 2 Posters 3.4k 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.
  • R Offline
    R Offline
    Rhadel
    wrote on last edited by
    #1

    Hi,

    I have a layout where I draw, when needed, a custom QWidget with some information about one of my object classes. When I click on another object, I would like to remove this custom QWidget, and draw on this layout another custom QWidget corresponding to the new clicked object.

    In order to remove and draw the new object, I've implemented this method, called when click signal is triggered:

    @
    void MainWindow::showBottomPanel(QWidget wid)
    {
    if (wid == 0) return;
    QLayout
    layout = ui->bottomLayoutMainPanel;
    if (layout == 0){ //Just some checks
    qDebug() << "No layout setted to frame. ";
    return;
    }
    //Clearing all elements in layout (manually).

    qDebug () << "Pointer to: " << wid;
    int i = 0;
    while (QLayoutItem* item = layout->itemAt(i)){
        if (item->widget()){
            layout->removeItem(item);
            // delete item; I DON'T DELETE MY OBJECT.
        }else i++;
    
    }
    layout->addWidget(wid);
    ui->bottomPanel->update();
    

    }
    @

    As you can see, I don't delete my custom widget object because if I reclick on the first object, I want to restore the QWidget and draw it again on the layout. So, I don't want to lose my object in memory.

    Problems: layout->removeItem(item); doesn't remove the item from layout. It continues stacking QWidgets again and again.

    How can I remove custom QWidgets object from a Layout, avoiding stacking, and saving the object in memory to redraw it again if needed?

    Thank you.

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

      Hi,

      Since you want to reuse your widgets, why not make use of "QStackedLayout":http://qt-project.org/doc/qt-4.8/qstackedlayout.html ? That would simplify the handling a lot

      Hope it helps

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

        Thank you SGaist, for your fast reply.

        If I'm not wrong, QStakedLayout uses arrows to navigate between objects. This is not exactly what I want, but I can works if I can set manually the active widget by clicking the custom QGraphicObject in scene.

        Anyway, I found this other solution:
        @
        bool found = false;
        while (QLayoutItem* item = layout->itemAt(i)){
        if (item->widget()){
        QWidget* actual = item->widget();
        if (actual == wid){ //Wid is the only object that i want to see
        item->widget()->showFullScreen();
        found = true;
        }else{
        item->widget()->hide();
        }
        i++;
        }
        }
        if (!found){
        layout->addWidget(wid);
        wid->hide();
        wid->showFullScreen();
        }
        @

        wid* is the only object that I want to see. The others are hidden. My second question is: is it a good and clean solution? I have the impression that is a botched job.

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

          You're welcome !

          But you are wrong, QStackedLayout (like the name says) is a layout so nothing to navigate is shown. I think you are mixing it with, maybe, QTabWidget.

          This layout puts your widgets on top of another showing only the current, which, if I understood your question right, is what you try to accomplish

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

            That's it! I was mixing with QTabWidget, sorry. I'm just too new in Qt. Tomorrow I'm trying your suggestion, but it seems just that I need.

            Just to know, anyway, is it a clean solution showing and hidding as I show at code above?

            Thank you again, SGaist.

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

              You're welcome.

              The code in itself doesn't look bad, but I never tried to call showFullScreen on a "layouted" widget so I don't know how it will react.

              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
              • R Offline
                R Offline
                Rhadel
                wrote on last edited by
                #7

                What about using just "show()"? It's a bit strange there is no methods like QWidget::clear() or something like that.

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

                  The opposite of show() is hide(). But really, go with the QStackedLayout, more intuitive and it handle everything for you.

                  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
                  • R Offline
                    R Offline
                    Rhadel
                    wrote on last edited by
                    #9

                    Thank you! Solved!

                    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