在QTableWidget中添加QCheckBox并使其居中显示(转)


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

复制代码
[cpp] view plain copy
 
  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的指针的方法

[cpp] view plain copy
 
  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

 

转自: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);


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM