I have been studying how to implement QStackedWidget
and QListWidget
and how the choice of a list inside QListWidget
affect the widget to show on the stack widget. After successfully going through this post I understand the basic of the QStackedWidget
. I wanted to pass to a more detailed OOP example and use .ui
forms to show the right selection of the QWidget
on a QStackedWidget
based on the selection on the QListWidget
.
The problem: From the image below as I select the first choice nothing happens and nothing happens if I select "Sonar Page", but when I select the last choice "Output Page" I see the widget on the QStackedWidget
, but it should appartain to the first choice. Signals of QListWidget::currentRowChanged
does not seem to be triggered correctly. Why is that happening? Basically seems to be triggered only the last choice instead all the others.
If I chose the last one I see the form but it should appear when I do the first selection "Vessel Positioning System Page"
main.cpp
#include "optionsdialog.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
OptionsDialog w;
w.show();
return a.exec();
}
optionsdialog.cpp
#include <QDialog>
#include "vesselpossystemwidget.h"
#include "sonarform.h"
#include "outputform.h"
class OptionsDialog;
}
class OptionsDialog : public QDialog
{
Q_OBJECT
public:
explicit OptionsDialog(QWidget *parent = nullptr);
~OptionsDialog();
// disabled
OptionsDialog(const OptionsDialog&) = delete;
OptionsDialog& operator=(const OptionsDialog&) = delete;
private:
Ui::OptionsDialog *ui;
VesselPosSystemWidget *mVesPos;
SonarForm *mSonar;
OutputForm *mOutput;
};
#endif // OPTIONSDIALOG_H
optionsdialog.cpp
#include "optionsdialog.h"
#include "ui_optionsdialog.h"
OptionsDialog::OptionsDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::OptionsDialog)
{
ui->setupUi(this);
mVesPos = new VesselPosSystemWidget;
mSonar = new SonarForm;
mOutput = new OutputForm;
ui->stackedWidget->addWidget(mVesPos);
ui->stackedWidget->addWidget(mSonar);
ui->stackedWidget->addWidget(mOutput);
QObject::connect(ui->listWidget, &QListWidget::currentRowChanged,
ui->stackedWidget, &QStackedWidget::setCurrentIndex);
}
OptionsDialog::~OptionsDialog()
{
delete ui;
}
sonarform.h
#include <QWidget>
namespace Ui {
class SonarForm;
}
class SonarForm : public QWidget
{
Q_OBJECT
public:
explicit SonarForm(QWidget *parent = nullptr);
~SonarForm();
// disabled
SonarForm(const SonarForm&) = delete;
SonarForm& operator=(const SonarForm&) = delete;
private:
Ui::SonarForm *ui;
};
#endif // SONARFORM_H
sonarform.cpp
#include "sonarform.h"
#include "ui_sonarform.h"
SonarForm::SonarForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::SonarForm)
{
ui->setupUi(this);
}
SonarForm::~SonarForm()
{
delete ui;
}
Ways I tried to solve the issue:
Also from the same source is clear how children should be passed for inheritance, and that is exactly what I did using the .ui
form. However I was not fully able to understand clearly why this process didn't work exactly how I expected. All the widgets are QWidgets
, but the main dialog is a QDialog
do that inheritance is taked care of.
The problem seems to be the QListWidget::currentRowChanged
signal of the QListWidget
. I tried also the following approach to solve the problem but did not work as well:
Second way I tried to solve the error
In the attempt below I used the SIGNAL
on_listWidget_currentRowChanged
to clearly trigger the choice on the list but that didn't work too:
optionsdialog.h
class OptionsDialog : public QDialog
{
Q_OBJECT
public:
explicit OptionsDialog(QWidget *parent = nullptr);
~OptionsDialog();
private slots:
void on_listWidget_currentRowChanged(int currentRow);
private:
Ui::OptionsDialog *ui;
VesselPosSystemWidget *mVesPos;
SonarForm *mSonar;
OutputForm *mOutput;
};
#endif // OPTIONSDIALOG_H
optionsdialog.cpp
OptionsDialog::OptionsDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::OptionsDialog)
{
ui->setupUi(this);
mVesPos = new VesselPosSystemWidget;
mSonar = new SonarForm;
mOutput = new OutputForm;
ui->stackedWidget->addWidget(mVesPos);
ui->stackedWidget->addWidget(mSonar);
ui->stackedWidget->addWidget(mOutput);
connect(ui->listWidget, &QListWidget::currentRowChanged,
[=](int index) { on_listWidget_currentRowChanged(index); });
}
OptionsDialog::~OptionsDialog()
{
delete ui;
}
void OptionsDialog::on_listWidget_currentRowChanged(int currentRow)
{
ui->stackedWidget->setCurrentIndex(currentRow);
}
In addition to that I am sure and can confirm that QListWidget::currentRowChanged
signal of the QListWidget
is correct and according to the proper notation because I followed official documentation updated to latest notation.
Please point to the right direction on how to solve this error and solving this issue.
Aucun commentaire:
Enregistrer un commentaire