In order to replicate the problem I have I prepared a small verifiable example.
I have 2 QStackedWidgets
inside a QGroupBox
with a couple more components as shown below:
I created another widget called QBoxForm
which carries a QComboBox
only.
This last widget should appear on the QStackedWidget
on the left as soon as the QCheckbox
is ticked.
The QStackedWidget
receive something because it becomes bigger but it does not show the QComboBox
. How to make sure that a component is fully visible inside a QStackedWidget
?
mainwindow.cpp
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
mCombo = new CBoxForm;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_checkBox_toggled(bool checked)
{
if(ui->checkBox->isChecked())
{
if(checked)
{
ui->stackedWidget->insertWidget(0, mCombo);
ui->stackedWidget->show();
}
}
if(!ui->checkBox->isChecked())
{
ui->stackedWidget->removeWidget(mCombo);
}
}
mainwindow.h
#include <QMainWindow>
#include "cboxform.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_checkBox_toggled(bool checked);
private:
Ui::MainWindow *ui;
CBoxForm *mCombo;
};
Lastly the additional combobox handled by another widget:
cboxform.h
#include <QWidget>
namespace Ui {
class CBoxForm;
}
class CBoxForm : public QWidget
{
Q_OBJECT
public:
explicit CBoxForm(QWidget *parent = nullptr);
~CBoxForm();
private:
Ui::CBoxForm *ui;
};
cboxform.cpp
CBoxForm::CBoxForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::CBoxForm)
{
ui->setupUi(this);
}
CBoxForm::~CBoxForm()
{
delete ui;
}
What I have done so far:
1) I followed the official documentation and applied the method insertWidget()
as advised. In fact here is what I have done exactly. This part works and can be confirmed by the fact that the QStackedWidget
become larger as I check the box.
Also consequently I remove the widget in a similar way applying removeWidget() method available in the official documentation.
Everything seems to follow the correct functioning, but the only missing part is that I don't understand why the QComboBox
does not appear on the QStackedWidget
as I followed precisely the official documentation.
Any idea on what I might be missing or forgot to include in the above code? Thanks for pointing in the right direction for solving this problem.
You should add ui->stackedWidget->setCurrentIndex(0) after ui->stackedWidget->insertWidget(0, mCombo) to make it visible.