replace one widget with another on a layout with a button

  • I am trying to replace a qscrollarea with a qgroupbox which contains two qlabels that are images of charts. I have tried the following but nothing happens.

    QGroupBox *videoChart = new QGroupBox;
    QHBoxLayout *videoChartLayout = new QHBoxLayout;

  • Qt Champions 2017

    Is that the actual code ?

    videoChart = new QGroupBox;

    but you add

    and what is startTest->clicked() ?

  • @mrjj when I press that button, i would like for the charts to replace the QScrollArea that is there right now. And that is not the whole whole code that it is the section I am trying to say when I click this button replace this with this

  • Qt Champions 2017


    Hi. I understand what you want to happen, but code seems
    strange. (to me)


    Confused me a bit if startTest is a button. ?

    the "clicked" is a signal and should be connected to a slot that will
    be called when the buttons is clicked.
    calling it directly will not work.

    Please place a QPushbutton on the form, then right click it and
    select Go to Slot, then select released().
    you will then go to a function.
    Put you code here.

    you can also connect stuff in code:

  • I have created everything from scratch so nothing is in the ui file. I have tried using SIGNAL and SLOTS but it didn't seem to recognize the widgets I wanted to replace

  • Qt Champions 2017

    well in the sample you call the groupbox for videoChart
    but in the addwidget you have "charts" ?
    I would expect it to be videoChart ?

    Well lets assume you replace function is called
    ReplaceNow and lives in mainwindow
    and your button is called startTest
    then you should have to have a statement like

    connect( startTest, SIGNAL(clicked()), this, SLOT(ReplaceNow()));

    for the button to do anything when clicked.
    Not sure if that is your issue ?

  • @mrjj chart is also a QGroupBox which I didn't include in the code. Sorry. Ok so how will it know what exact widgets to replace? That is my question. Would it be something like this?

    void ReplaceNow()

  • Qt Champions 2017

    Ahh. ok.
    yes then you would use the pointer to the widget.
    Like instructionsScrollArea1 if that was the one you used to insert it with in first place?

  • Lifetime Qt Champion


    Sounds rather like a job for QStackedLayout

  • @mrjj I did this but it says that the variables are not declared on the scope

    void ReplaceNow()

  • Qt Champions 2017


    instructionsScrollArea1 and ReplaceNow() should be
    in same object or same file at least.

    that what scope means.

    so where is instructionsScrollArea1 declared and where
    is void ReplaceNow() ?

    the * in
    is wrong.

    If possible,post whole code here and its easier for us to help.

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.