How to properly show the right user interface on the QStacked Widget
depending on the choice on QListWidget
?
Below I have a QDialog
that contains:
1 QListWidget
1 QStackedWidget
Depending on the selected choice of the list, for example, Vessel Position System, or Output the interface should look like below, but the problem is that unfortunately the QStacked Widget
is currently not changing the QWidgets
as I select QListWidgets
, actually is not showing anything. A Minimal Verifiable Example can be found here. Just clone it and it will work:
Below the code:
optionsdialog.h
#include <QDialog>
#include "vesselpossystemwidget.h"
#include "outputdialog.h"
#include "sonarsystem.h"
namespace Ui {
class OptionsDialog;
}
class OptionsDialog : public QDialog
{
Q_OBJECT
public:
explicit OptionsDialog(QWidget *parent = nullptr);
~OptionsDialog();
private:
Ui::OptionsDialog *ui;
VesselPosSystemWidget *mVesPos;
OutputDialog *mOutput;
SonarSystem *mSonar;
};
#endif // OPTIONSDIALOG_H
optionsdialog.h
#include "optionsdialog.h"
#include "ui_optionsdialog.h"
OptionsDialog::OptionsDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::OptionsDialog)
{
ui->setupUi(this);
switch(ui->stackedWidget->currentIndex()){
case 0:
// Go to positioning system
mVesPos = new VesselPosSystemWidget();
mVesPos->show();
break;
case 1:
// Go to sonar set up...
mSonar = new SonarSystem();
mSonar->show();
break;
case 2:
// Go to output
mOutput = new OutputDialog();
mOutput->show();
break;
default:
break;
}
}
OptionsDialog::~OptionsDialog()
{
delete ui;
}
vesselposwidget.h
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui { class VesselPosSystemWidget; }
QT_END_NAMESPACE
class VesselPosSystemWidget : public QWidget
{
Q_OBJECT
public:
VesselPosSystemWidget(QWidget *parent = nullptr);
~VesselPosSystemWidget();
private:
Ui::VesselPosSystemWidget *ui;
};
#endif // VESSELPOSSYSTEMWIDGET_H
vesselposwidget.cpp
#include "vesselpossystemwidget.h"
#include "ui_vesselpossystemwidget.h"
VesselPosSystemWidget::VesselPosSystemWidget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::VesselPosSystemWidget)
{
ui->setupUi(this);
}
VesselPosSystemWidget::~VesselPosSystemWidget()
{
delete ui;
}
output.h
#include <QDialog>
namespace Ui {
class OutputDialog;
}
class OutputDialog : public QDialog
{
Q_OBJECT
public:
explicit OutputDialog(QWidget *parent = nullptr);
~OutputDialog();
private:
Ui::OutputDialog *ui;
};
#endif // OUTPUTDIALOG_H
output.cpp
#include "outputdialog.h"
#include "ui_outputdialog.h"
OutputDialog::OutputDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::OutputDialog)
{
ui->setupUi(this);
}
OutputDialog::~OutputDialog()
{
delete ui;
}
What I have done so far:
I am exploring the possibility to use a QStacked Widget
as I am not very familiar with it and wanted to practice with a basic example. I was investigating this source and also this one
Also from the same source there is more documentation that I read. However I was not fully able to understand clearly the use of this particular widget.
In addition to that I tried a switch - case
loop as I shown above on the code but I Am not sure why it didn't work
Can anyone please provide some guidance on how to properly show the right user interface on the QStacked Widget
depending on the choice on QListWidget
? Any direction for solving this issue is appreciated.