qt 如何使用按钮动态添加包含一些小部件的组框?

qt how to add a groupbox that contains some widgets dynamically with a pushbutton?

我有一个包含一些按钮和滑块的组框。我希望当我点击一个按钮时,一个与前一个相同的新组框应该出现在第一个按钮下面。每当我点击按钮时,同样的情况应该会动态发生。因为我需要多达 32 个组框,所以我不想手动放置所有组框。那么,我该怎么做呢?

首先,强烈推荐布局。

这是一个例子(我以前做过)。您可以从 QScrollArea 派生一个 class,然后在构造函数中设置您想要的布局。

这里有一个名为 Add 的简单按钮位于 window 中。 如果按下它,将添加一行并使用默认值 (0, 0, 0) <- integers 进行初始化。 在实时程序中,我从 file/database 加载值然后初始化它。

您可能想使用不同的布局和不同的设置,但这应该能让您有所了解。我敢肯定,通过更多的实验,您会到达想要的位置。

//Structure to keep track of the added widgets easier
struct ItemRow
{
    ItemRow(QLineEdit *entry, QLineEdit *amount, QComboBox *box)
        : m_Entry(entry)
        , m_Amount(amount)
        , m_Box(box)
    { }

    ItemRow(void)
        : m_Entry(nullptr)
        , m_Amount(nullptr)
        , m_Box(nullptr)
    { }

    QLineEdit *m_Entry;
    QLineEdit *m_Amount;
    QComboBox *m_Box;
};

class声明。

class MyScrollArea : public QScrollArea
{
    Q_OBJECT

public:
    explicit MyScrollArea(QWidget *parent = 0);
    ~MyScrollArea();
    //...
    void OnAddButtonPressed(void);
    void DrawButtonLayout(void);
    void AddRow(int val1, int val2, int val3); //Use own parameters

private:
    QVBoxLayout *m_LayoutFirstRow;
    QVBoxLayout *m_LayoutSecondRow;
    QVBoxLayout *m_LayoutThirdRow;
    //...
    QVBoxLayout *m_LayoutButton;
    //...
    QList<QPushButton*> m_Buttons;
    QVector<ItemRow> m_ItemRows;
}

实施。

MyScrollArea::MyScrollArea(QWidget *parent) :
    QScrollArea(parent),
    ui(new Ui::MyScrollArea)
{
    ui->setupUi(this);
    setWidget(new QWidget);
    setWidgetResizable(true);
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);

    QHBoxLayout *mainLayout = new QHBoxLayout(this);

    m_LayoutFirstRow    = new QVBoxLayout();
    m_LayoutSecondRow   = new QVBoxLayout();
    m_LayoutThirdRow    = new QVBoxLayout();
    m_LayoutButton      = new QVBoxLayout();

    widget()->setLayout(mainLayout);

    mainLayout->addLayout(m_LayoutFirstRow);
    mainLayout->addLayout(m_LayoutSecondRow);
    mainLayout->addLayout(m_LayoutThirdRow);
    mainLayout->addLayout(m_LayoutButton);

    DrawButtonLayout();
}

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

void MyScrollArea::OnAddButtonPressed(void)
{
    AddRow(0, 0, 0);
}

void MyScrollArea::DrawButtonLayout(void)
{
    QPushButton *addBtn = new QPushButton("Add");
    connect(addBtn, SIGNAL(clicked()), this, SLOT(OnAddButtonPressed()));
    m_LayoutButton->addWidget(addBtn);
    m_Buttons.push_back(addBtn); //Keep somewhere track of the button(s) if needed - example: put in QList (not the best approach though)
}

void MyScrollArea::AddRow(int val1, int val2, int val3)
{
    QLineEdit *pEntry = new QLineEdit(QString::number(val1));
    pEntry->setValidator(new QIntValidator());
    QLineEdit *pAmount = new QLineEdit(QString::number(val2));
    pAmount->setValidator(new QIntValidator());
    QComboBox *pBox = new QComboBox();
    InitComboBox(pBox, val3); //Initialize the combo-box (use connect if you wish) - code not included

    m_LayoutFirstRow->addWidget(pEntry);
    m_LayoutSecondRow->addWidget(pAmount);
    m_LayoutThirdRow->addWidget(pBox);

    ItemRow row;
    row.m_Entry = pEntry;
    row.m_Amount = pAmount;
    row.m_Box = pBox;
    m_ItemRows.push_back(row);
}

如果有什么不对的地方请留言,我在Notepad++里整理的。

注意:文档-link是针对QT4.8的,因为5.3已经不可用了,不过我的代码也是5.3版本的。