This can be done a lot easier IMO:
//the tablewidget
//some rows and columns
tableWidget->setRowCount(3);
tableWidget->setColumnCount(3);
//first create a new widget
//lets say we want a checkbox in the cell
// create the layout ON THE HEAP! (else after the function ends, layout is gone)
// add the checkbox to the widget
layout->addWidget(box);
// center align the layout (box ends up in the center horizontally and vertically)
layout->setAlignment( Qt::AlignCenter );
// set the layout on the widget, it takes ownership of the layout (don't need to delete it later)
wdg->setLayout(layout);
// set the widget in the cell
tableWidget->setCellWidget(2, 0, wdg);
//the tablewidget
QTableWidget* tableWidget = new QTableWidget(w);
//some rows and columns
tableWidget->setRowCount(3);
tableWidget->setColumnCount(3);
//first create a new widget
QWidget* wdg = new QWidget;
//lets say we want a checkbox in the cell
QCheckBox *box = new QCheckBox();
// create the layout ON THE HEAP! (else after the function ends, layout is gone)
QHBoxLayout* layout = new QHBoxLayout(wdg);
// add the checkbox to the widget
layout->addWidget(box);
// center align the layout (box ends up in the center horizontally and vertically)
layout->setAlignment( Qt::AlignCenter );
// set the layout on the widget, it takes ownership of the layout (don't need to delete it later)
wdg->setLayout(layout);
// set the widget in the cell
tableWidget->setCellWidget(2, 0, wdg);
To copy to clipboard, switch view to plain text mode
Bookmarks