replace one widget with another on a layout with a button
-
wrote on 2 Oct 2015, 17:04 last edited by
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);
-
Hi
Is that the actual code ?videoChart = new QGroupBox;
but you add
addWidget(charts,0,Qt::AlignHCenter);and what is startTest->clicked() ?
-
Hi
Is that the actual code ?videoChart = new QGroupBox;
but you add
addWidget(charts,0,Qt::AlignHCenter);and what is startTest->clicked() ?
wrote on 2 Oct 2015, 18:51 last edited by marlenet15 10 Feb 2015, 18:56@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
-
@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 -
wrote on 2 Oct 2015, 19:22 last edited by marlenet15 10 Feb 2015, 19:22
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
-
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
Lifetime Qt Championwrote on 2 Oct 2015, 19:33 last edited by mrjj 10 Feb 2015, 19:35@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 ? -
@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 ?wrote on 2 Oct 2015, 19:46 last edited by@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); }
-
@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
-
@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?wrote on 2 Oct 2015, 20:10 last edited by@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); }
-
@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); }
Lifetime Qt Championwrote on 2 Oct 2015, 21:12 last edited by mrjj 10 Feb 2015, 21:22instructionsScrollArea1 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.
1/11