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;
    videoChartLayout->addWidget(leftVideos2);
    videoChartLayout->addWidget(rightVideos2);
    if(startTest->clicked())
    {
        videoChartLayout->removeWidget(instructionsScrollArea1);
        videoChartLayout->addWidget(charts,0,Qt::AlignHCenter);
    }
    else
    {
        videoChartLayout->addWidget(instructionsScrollArea1);
    }
    
    videoChartLayout->setStretch(0,2);
    videoChartLayout->setStretch(1,2);
    videoChartLayout->setStretch(2,6);
    videoChart->setLayout(videoChartLayout);

  • Qt Champions 2016

    Hi
    Is that the actual code ?

    videoChart = new QGroupBox;

    but you add
    addWidget(charts,0,Qt::AlignHCenter);

    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 2016

    @marlenet15

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

    the
    if(startTest->clicked())

    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:
    http://doc.qt.io/qt-5/signalsandslots.html



  • 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 2016

    @marlenet15
    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()
    {
     videoChartLayout->removeWidget(instructionsScrollArea1);
     videoChartLayout->addwidget(charts,0,Qt::AlignHCenter);
    }

  • Qt Champions 2016

    @marlenet15
    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

    Hi,

    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()
    {
     *videoChartLayout->removeWidget(*instructionsScrollArea1);
     *videoChartLayout->addwidget(charts,0,Qt::AlignHCenter);
    }

  • Qt Champions 2016

    @marlenet15

    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() ?

    Also
    the * in
    removeWidget(*instructionsScrollArea1);
    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.