2

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

4

1 回答 1

2

首先,强烈推荐布局。

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

Add在这里,窗口中有一个简单的按钮。如果您按下它,则会添加一行并使用默认值进行初始化(0, 0, 0) <- integers。在实时程序中,我从文件/数据库中加载值并对其进行初始化。

您可能想要使用不同的布局和不同的设置,但这应该会给您一个想法。我敢肯定,通过更多的实验,你会得到你想要的。

//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 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++ 中。

注意:文档链接适用于 QT4.8,因为 5.3 不再可用,但我的代码也来自 5.3 版。

于 2015-09-03T09:37:22.487 回答