Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Cell padding problem while centering a checkbox in QTableWidget

Cell padding problem while centering a checkbox in QTableWidget

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 2.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Binary91
    wrote on last edited by VRonin
    #1

    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?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      KidTrent
      wrote on last edited by
      #2

      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);
      
      1 Reply Last reply
      0
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @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.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved