replace one widget with another on a layout with a button
-
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
-
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
-
@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 likeconnect( 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); }
-
@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? -
Hi,
Sounds rather like a job for QStackedLayout
-
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.