Search code examples
c++qtqt5qtablewidgetqtablewidgetitem

Is it possible to add a QTableWidget inside another QTableWidget?


I have a QTableWidget where I would like to populate on the fields with another QTableWidget.

This is something it would look like:

--------------------------
Name | Class | Section    |
---------------------------
     |       | Some Text  |
 A   |  1    | CheckBox 1 |
     |       | CheckBox 2 |
---------------------------

I want to add a Table B in each of the fields in the section column so that it is easier for me to deal with each checkboxes for each item in Table A (main Table).

Is it possible? Are there any better ways to approach this problem?


Solution

  • Use custom widget instead:

    QWidget *wgt = new QWidget();
    QVBoxLayout *hlayout = new QVBoxLayout;
    QCheckBox *check1 = new QCheckBox("1");
    QCheckBox *check2 = new QCheckBox("2");
    QLineEdit *lineEdit = new QLineEdit;
    lineEdit->setText("test");
    hlayout->addWidget(lineEdit);
    hlayout->addWidget(check1);
    hlayout->addWidget(check2);
    wgt->setLayout(hlayout);
    ui->tableWidget->setCellWidget(0,2,wgt);
    ui->tableWidget->resizeRowsToContents();
    

    With setCellWidget you can also add another QTableWidget but I think that QWidget will be enough.

    You can add for example: lineEdit->setStyleSheet("border:0px"); and your lineEdit will be without borders or use just QLabel instead, it is widget, do all what you need.

    Result:

    enter image description here