Cell padding problem while centering a checkbox in QTableWidget
-
Hello,
I try to add checkboxes to every cell of my QTableWidget, like this:
int main(int argc, char** args) { FreeConsole(); QApplication app(argc, args); QWidget window; QVBoxLayout layout(&window); QTableWidget table_main; table_main.setStyleSheet("QTableWidget::item { padding: 0px }"); // <--- not solving the problem table_main.setColumnCount(9); table_main.setRowCount(10); QStringList table_main_headers_h; table_main_headers_h << "Amount" << "Person 1" << "Person 2" << "Person 3" << "Person 4" << "Person 5"; table_main.setHorizontalHeaderLabels(table_main_headers_h); for(int i = 0;i < table_main.rowCount(); i++) { for(int ii = 1; ii < table_main.columnCount(); ii++) { QWidget *table_main_cell_widget = new QWidget(); table_main_cell_widget->setContentsMargins(0,0,0,0); // <--- also not solving the problem QHBoxLayout *table_main_cell_layout = new QHBoxLayout(table_main_cell_widget); table_main_cell_layout->setAlignment(Qt::AlignCenter); QCheckBox *table_main_cb = new QCheckBox(); table_main_cell_layout->addWidget(table_main_cb); table_main_cell_widget->setLayout(table_main_cell_layout); // <--- second Question: why do I need this command? There is no change if I don't use it... table_main.setCellWidget(i, ii, table_main_cell_widget); } } layout.addWidget(&table_main); window.setWindowTitle("Calculator"); window.showMaximized(); app.exec(); return 0; }
The problem is that the checkboxes are not fully displayed (left & right border are shown, but top and bottom border are missing). When I manually change the vertical cell size with mouse, then the top and bottom borders of the checkboxes in this row appear...
How can I manage it that my widgets inside the table cells are also vertically shown in full size?
-
Ran into the same issue, and figured I'd post a reply since there was 1.1k views and not a single answer...
The reason is due to the layout margins,. So in this users case, using the following would solve the problem:
table_main_cell_layout->setMargins(0);
-
@KidTrent said in Cell padding problem while centering a checkbox in QTableWidget:
views and not a single answer...
Because it was answered already a lot of times elsewhere - Qt does not support it out of the box and the best is to use a custom item delegate and paint it by yourself.