Qt World Summit: Submit your Presentation

Updating a Widget and repainting

  • I am designing a UI made up of 3 components; a header panel across the top, a button panel down the right side, and a main content panel taking up the rest of the screen, like so:

    The Header and the Button Panel stay the same, but the content panel should switch between a couple different widgets. Is there a way to replace and repaint the widget on the screen without adding it to the layout again?

    This is the code I use to layout my widgets:

    mainLayout = new QGridLayout;
    mainLayout->addWidget(headerPnl, 0, 0, 1, 7);
    mainLayout->addWidget(contentPnl, 1, 0, 10, 6);
    mainLayout->addWidget(buttonPnl, 1, 6, 10, 1);

    This was my attempt to implement a setContentPnl method, but it doesn't seem to work. Nothing is replaced and the UI doesn't change.

    void MainWidget::setContentPnl(QWidget *content){
        contentPnl = content;

    Is there a way I can point my contentPnl to different widgets, and update the screen accordingly?

  • Lifetime Qt Champion

    When you say " switch between a couple different widgets"
    you mean like flipping pages ?
    In that case, http://doc.qt.io/qt-5/qstackedwidget.html
    will make that easy.

    if you mean to actually replace the widget, you have to take it from layout and
    insert new one.

    Can you explain why u need to replace the widget ?
    is it sort of a preview of what is selected in Button panel or
    what does it do for app?

  • @mrjj I mean to actually replace it. The app is meant to keep the header and button panel static, ie always there, always in the same place, and always looking the same. The only part of the screen that changes (as a result of different buttons being pressed) is the content panel.

    I suppose I will have to keep a reference to the layout and just remove and add when necessary. Thanks.

  • Lifetime Qt Champion

  • @Smeeth
    But why not use a QStackedWidget for the Main Content Panel?

Log in to reply