How can I add a checkbox/radio button to QTableWidget

后端 未结 3 1960
逝去的感伤
逝去的感伤 2020-12-25 11:44

How can I add a checkbox/radiobutton/combobox to a QTableWidget or a QListWidget?

3条回答
  •  既然无缘
    2020-12-25 12:10

    you can add checkbox like this too

    #include 
    
    void addCheckBoxAt(int row_number, int column_number,int state)
    {
    
        // Create a widget that will contain a checkbox
         QWidget *checkBoxWidget = new QWidget();
         QCheckBox *checkBox = new QCheckBox();      // We declare and initialize the checkbox
         QHBoxLayout *layoutCheckBox = new QHBoxLayout(checkBoxWidget); // create a layer with reference to the widget
         layoutCheckBox->addWidget(checkBox);            // Set the checkbox in the layer
         layoutCheckBox->setAlignment(Qt::AlignCenter);  // Center the checkbox
         layoutCheckBox->setContentsMargins(0,0,0,0);    // Set the zero padding
         /* Check on the status of odd if an odd device,
           * exhibiting state of the checkbox in the Checked, Unchecked otherwise
           * */
    
          if(state == 1){
              checkBox->setChecked(true);
          } else {
              checkBox->setChecked(false);
          }
          ui->job_table_view->setCellWidget(row_number,column_number, checkBoxWidget);
    
    
         // Another way to add check box as item
        /*
    
       // QTableWidgetItem *checkBoxItem = new QTableWidgetItem("checkbox string ");
        QTableWidgetItem *checkBoxItem = new QTableWidgetItem();
        checkBoxItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
        checkBoxItem->setCheckState(Qt::Checked);
        ui->job_table_view->setItem(row_number,column_number,checkBoxItem);
    
        */
    }
    

    // call it like

    addCheckBoxAt(0,0,1);  // insert checkbox it 0,0 and check status true
    

提交回复
热议问题