Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ qt如何添加一个包含一些带有按钮的动态小部件的groupbox?_C++_Qt_Qt Designer_Qt5.3_Qgroupbox - Fatal编程技术网

C++ qt如何添加一个包含一些带有按钮的动态小部件的groupbox?

C++ qt如何添加一个包含一些带有按钮的动态小部件的groupbox?,c++,qt,qt-designer,qt5.3,qgroupbox,C++,Qt,Qt Designer,Qt5.3,Qgroupbox,我有一个groupbox,其中包含一些按钮和滑块。我希望当我点击一个按钮时,第一个按钮下会出现一个与前一个相同的新groupbox。每当我点击按钮,同样的情况就会动态发生。因为我需要多达32个这样的groupbox,所以我不想手动放置所有groupbox。那么,我如何才能做到这一点呢?首先,强烈建议使用布局 下面是一个例子(我以前做过)。您可以从中派生类,然后在构造函数中设置您想要的布局 在这里,窗口中有一个名为Add的简单按钮。 如果您按下它,一行将被添加并初始化为默认值(0,0,0)您是否曾

我有一个groupbox,其中包含一些按钮和滑块。我希望当我点击一个按钮时,第一个按钮下会出现一个与前一个相同的新groupbox。每当我点击按钮,同样的情况就会动态发生。因为我需要多达32个这样的groupbox,所以我不想手动放置所有groupbox。那么,我如何才能做到这一点呢?

首先,强烈建议使用布局

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

在这里,窗口中有一个名为
Add
的简单按钮。
如果您按下它,一行将被添加并初始化为默认值
(0,0,0)您是否曾在
c++
中动态创建过某个内容?您需要创建一个带有信号和slotsI的事件,我会放置一个listwidget并在其中动态插入文件名@胜利者。那么,我应该在C++中写一个插槽函数,还是你提到了一些不同的东西?还有,我应该创建哪种事件类型?您所需要的只是在
c++
code中动态创建小部件,并将它们放入相同的父小部件中。你有很多方法可以做到这一点。但是我如何安排小部件的位置,因为我想在另一个小部件下添加新的小部件?我已经理解了,但我还有一个问题。我有一个groupbox包含一些滑块和按钮。当我动态添加新的groupbox时,我应该手动添加所有滑块和按钮还是有更好的解决方案?只需像我做的那样创建一个函数
AddRow
,或者将其命名为
AddGroupBox
,然后在函数中添加一个新的组框和控件。因此,您只需在外部执行对add函数的简单调用。(有无参数,由您决定)您也可以在分组框中添加布局。感谢您提供的良好解决方案。那么这是最后一个问题。我放置了一个布局,然后是一个滚动区域。但首先我的groupbox覆盖了所有布局,然后每当我添加一个新的groupbox时,它们的大小都在减小,并且仍然没有滚动条。有什么问题?您是否使用了
setwidgetsizeable
并设置了它的大小策略?我将setWidgetResizeable设置为on,并将大小策略设置为preferred。这是真的还是我应该让他们做些别的?
//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);
}