標籤:
實現思路:把QCheckBox嵌入式到一個水平布局中
[cpp] view plaincopy
- QWidget *widget;
- QHBoxLayout *hLayout;
- QCheckBox *ckb;
- ...
- ckb = new QCheckBox();
- hLayout = new QHBoxLayout();
- widget = new QWidget(ui->tableWidget);
- hLayout->addWidget(ckb);
- hLayout->setMargin(0); // 必須添加, 否則CheckBox不能正常顯示
- hLayout->setAlignment(ckb, Qt::AlignCenter);
- widget->setLayout(hLayout);
- ...
- ui->tableWidget->setCellWidget(row, column, widget);
擷取CheckBox的指標的方法
[cpp] view plaincopy
- QWidget *widget = (QWidget *)ui->tableWidget->cellWidget(row, column);
- QCheckBox *ckb = (QCheckBox *)widget->children().at(1);
- ckb->setChecked(true);
- ...
備忘: 可使用 qDebug() << widget->children(); 輸出widget的child列表 從而判斷CheckBox的index
轉自:http://blog.csdn.net/it_mac/article/details/8953191
-------------------------------------------------------------------------自學筆記-------------------------------------------------------------------------
//開關按鈕 QWidget* widget = new QWidget(); QToolButton* switchBtn = new QToolButton(widget); switchBtn->setFixedSize(QSize(nWidth, nHeight)); switchBtn->setText("關"); switchBtn->setStyleSheet(offStyle); QHBoxLayout* hLayout = new QHBoxLayout(widget); hLayout->setMargin(0); hLayout->addWidget(switchBtn); hLayout->setAlignment(switchBtn, Qt::AlignCenter); devTableWidget->setCellWidget(row, column, widget); connect(switchBtn, SIGNAL(clicked()), switchMapper, SLOT(slot_switchBtn()));
//擷取按鈕指標 QToolButton*toolBtn=(QToolButton*)devTableWidget->cellWidget(row,column)->children().at(0);
在QTableWidget中添加QCheckBox並使其置中顯示(轉)