lundi 23 mars 2020

How to show proper QStackedWidget page after QListWidget selection

I have 1 QListWidget and 1 QStackedWidget. The QstackedWidget has three different widgets to be shown after selecting the entriesd on the QListWidget.

The problem: as I select the first choice nothing happens and nothing happens if I select the second choice, but when I select the last choice I see the widget on the QStackedWidget. This widget does not appartain to the third widget but it appartain to the first widget.

Signals of [QListWidget::currentRowChanged(C++ - QListWidget select first item) does not seem to be triggered correctly. Why is that happening? Basically seems to be triggered only the last choice instead all the others.

Below the snippet of code:

1 solution: works partially because out of three choices I can only see the last QWidget showing on the QStackedWidget:

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);

    ui->horizontalLayout->addWidget(ui->stackedWidget);
    setLayout(ui->horizontalLayout);


    QObject::connect(ui->listWidget, &QListWidget::currentRowChanged,
            ui->stackedWidget, &QStackedWidget::setCurrentIndex);
}

OptionsDialog::~OptionsDialog()
{
    delete ui;
}

2 solution: works partially because out of three choices I can only see the last QWidget showing on the QStackedWidget. This solution was taken from official documentation of QStackedWidget:

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);

    ui->horizontalLayout->addWidget(ui->stackedWidget);
    setLayout(ui->horizontalLayout);


    connect(ui->listWidget, QOverload<int>::of(&QListWidget::currentRowChanged),
        ui->stackedWidget, &QStackedWidget::setCurrentIndex);

}

OptionsDialog::~OptionsDialog()
{
    delete ui;
}

3 solution: same exact effect, only the third QWidget is shown:

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);

    ui->horizontalLayout->addWidget(ui->stackedWidget);
    setLayout(ui->horizontalLayout);


    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);
}

I don't know what else to try to have the .ui forms switch after selcting the item in the QListWidget. Is there something I am missing? I tried all possible connect combinations but despite that I can only show the third choice.

Aucun commentaire:

Enregistrer un commentaire