在QTableWidget中添加QCheckBox並使其置中顯示(轉)

來源:互聯網
上載者:User

標籤:

實現思路:把QCheckBox嵌入式到一個水平布局中

[cpp] view plaincopy 
  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 plaincopy 
  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);

在QTableWidget中添加QCheckBox並使其置中顯示(轉)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.