实现思路:把QCheckBox嵌入式到一个水平布局中

 1 QWidget *widget;
 2 QHBoxLayout *hLayout;
 3 QCheckBox *ckb;
 4 ...
 5 ckb = new QCheckBox();
 6 hLayout = new QHBoxLayout();
 7 widget = new QWidget(ui->tableWidget);
 8 hLayout->addWidget(ckb);
 9 hLayout->setMargin(0);                          // 必须添加, 否则CheckBox不能正常显示
10 hLayout->setAlignment(ckb, Qt::AlignCenter);
11 widget->setLayout(hLayout);
12 ...
13 ui->tableWidget->setCellWidget(row, column, widget);

 

获取CheckBox的指针的方法

1 QWidget *widget = (QWidget *)ui->tableWidget->cellWidget(row, column);
2 QCheckBox *ckb = (QCheckBox *)widget->children().at(1);
3 ckb->setChecked(true);
4 ...

 

备注:
    可使用 qDebug() << widget->children(); 输出widget的child列表
    从而判断CheckBox的index

 

相关文章:

  • 2021-11-13
  • 2021-05-31
  • 2022-12-23
  • 2021-11-11
  • 2022-12-23
  • 2021-10-29
  • 2022-03-04
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-11-22
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案