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. Correct way to replace widgets (without deleting them) in GUI
Qt 6.11 is out! See what's new in the release blog

Correct way to replace widgets (without deleting them) in GUI

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 1.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.
  • S Offline
    S Offline
    Smeeth
    wrote on last edited by Smeeth
    #1

    I am having trouble adding/removing and replacing widgets from my GUI.

    I have a class MainWindow, that is my main screen and contains the widgets that are being replaced.

    Here is how I set up my GUI in the constructor, using a private member variable QGridLayout mainLayout:

    mainLayout = new QGridLayout;
    mainLayout->setMargin(0);
    mainLayout->setSpacing(0);
    mainLayout->addWidget(infoPnl, 0, 0, 1, 2);
    mainLayout->addWidget(contentPnl, 1, 0, 1, 1);
    mainLayout->addWidget(buttonPnl, 1, 1, 1, 1);
    

    This creates a panel across the top of the window, and two smaller widgets underneath.

    I have methods to replace the two panels along the bottom (contentPnl and buttonPnl);

    void MainWindow::setContentPane(ContentPanel *content){
        mainLayout->replaceWidget(contentPnl, content);
        contentPnl = content;
        mainLayout->update();
    }
    

    This doesn't work properly, as the new panels overwrite and cover up the existing ones. In this example, when I call setContentPanel(), the content panel is added to the GUI correctly but other elements (such as the infoPnl across the top) are covered.

    For some reason, if I add a statement to delete the old widget, this works fine and everything looks proper. I don't want to delete the widget, as I store the GUI components elsewhere and don't want them deleted.

    void MainWindow::setContentPane(ContentPanel *content){
        mainLayout->replaceWidget(contentPnl, content);
        delete contentPnl;
        contentPnl = content;
        mainLayout->update();
    }
    

    Why does deleting the widget change the GUI behavior? How can I achieve this affect without deleting the widget I am replacing?

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

      Hi,

      Aren't rather looking for something like QStackedWidget ?

      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,

        Aren't rather looking for something like QStackedWidget ?

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

        @SGaist I will look into it. This seems much easier and more intuitive than removing and adding things. Thank you

        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