如何在一个合并单元格中设置多个复选框?

How to set multiple checkboxes in one merged cell?

我需要在一个合并的单元格中设置多个复选框,以便让它们并排或一个在另一个下面(无关紧要)。现在我尝试时它们相互重叠。

这里是代码片段:

QTableWidget* my_table;
...
my_table->setSpan(0, 2, 2, 2);
...
my_table->setCellWidget(0,2, new QCheckBox("First"));
my_table->setCellWidget(1,2, new QCheckBox("Second"));

您必须创建一个具有布局(水平或垂直)的 QWidget,然后将复选框添加到布局中,如下所示:

QWidget *widget = new QWidget();
QHBoxLayout *horizontalLayout = new QHBoxLayout(widget);
QCheckBox *cb = new QCheckBox(widget);
cb->setText("First");
QCheckBox *cb2 = new QCheckBox(widget);
cb2->setText("Second");
widget ->layout()->addWidget(cb); //adding to layout the first checkbox
widget ->layout()->addWidget(cb2);//adding to layout the second checkbox

//Your code
my_table->setCellWidget(1,2, widget);