Qt如何添加一个组框,其中包含一些动态的小部件与一个按钮
qt how to add a groupbox that contains some widgets dynamically with a pushbutton?
我有一个组框,其中包含一些按钮和滑块。我希望,当我点击一个按钮,一个新的分组框是相同的,与前一个应该出现在第一个。当我点击按钮时,同样的情况会动态发生。因为我需要多达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版本。
相关文章:
- 你能在链表中有一个动态数组和它的内存分配吗
- 从另一个动态链接库项目调用静态库函数
- 指向结构中的数组的指针,其中每个字段都是一个动态数组
- 无法在 CMake 中创建动态库并将其链接到另一个动态库?
- 如何创建一个动态数组,该数组可以保存从C++中派生的同一基类的所有不同对象
- 创建一个动态2D数组来存储类对象
- C++中的特征库有一个动态向量或矩阵吗
- 我需要一个在C 中具有一个动态维度的二维数组
- 在一个动态分配的数组的程序中分割故障
- 创建一个动态数组,包括汽车的年份和品牌,但无法保存我的第一个输出
- 使用 memcpy 将动态数组的内容复制到另一个动态数组上会产生运行时错误
- 在类成员函数中创建一个动态数组
- 当动态创建一个包含另一个动态创建的结构数组的结构数组时,内存管理
- 在C 中,正在将一个动态分配的对象传递给一个函数(总是)一个坏主意
- 在MacOS上编译一个动态库,库搜索路径指向同一目录(CMake)
- 声明一个动态大小数组
- 创建一个动态分配的std::vectors数组
- 从数组中删除多个元素并创建一个动态数组c++
- 如何根据另一个动态库链接动态库
- 返回一个动态数组