Qt如何添加一个组框,其中包含一些动态的小部件与一个按钮

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

本文关键字:一个 动态 包含一 按钮 小部 何添加 添加 Qt      更新时间:2023-10-16

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

首先,强烈建议使用布局。

下面是一个例子(我以前这样做过)。你可以从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版本。