简体   繁体   English

如何在 QStackedWidget 中动态添加小部件

[英]How to dynamically add widget inside a QStackedWidget

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:我在QStackedWidgets中有 2 个QGroupBox ,还有几个组件,如下所示:

堆

I created another widget called QBoxForm which carries a QComboBox only.我创建了另一个名为QBoxForm的小部件,它只带有一个QComboBox This last widget should appear on the QStackedWidget on the left as soon as the QCheckbox is ticked.勾选QCheckbox QStackedWidget

影响

The QStackedWidget receive something because it becomes bigger but it does not show the QComboBox . QStackedWidget收到一些东西,因为它变大了,但没有显示QComboBox How to make sure that a component is fully visible inside a QStackedWidget ?如何确保组件在QStackedWidget中完全可见?

mainwindow.cpp主窗口.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主窗口.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:最后,由另一个小部件处理的附加 combobox :

cboxform.h 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.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. 1)我按照官方文档并按照建议应用了方法insertWidget() 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.这部分有效,并且可以通过我选中该框时QStackedWidget变大这一事实来确认。

Also consequently I remove the widget in a similar way applying removeWidget() method available in the official documentation.因此,我也以类似的方式删除了小部件,应用了官方文档中提供的removeWidget()方法。

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.一切似乎都遵循正确的功能,但唯一缺少的部分是我不明白为什么QComboBox没有出现在QStackedWidget上,因为我完全遵循了官方文档。

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.您应该在ui->stackedWidget->insertWidget(0, mCombo ) 之后添加ui->stackedWidget->setCurrentIndex(0 ) 以使其可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM