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. QTableWidget spaces between columns
Forum Update on Monday, May 27th 2025

QTableWidget spaces between columns

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 13.2k Views
  • 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.
  • E Offline
    E Offline
    evergreen
    wrote on 12 May 2011, 08:52 last edited by
    #1

    For a question of ergonomy, i want to add a space between 2 columns of my QTableWidget. I couldn't find a way in the doc.
    And adding an empty column makes things pretty ugly.

    Is this add possible?

    Thanks

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sigrid
      wrote on 13 May 2011, 07:54 last edited by
      #2

      In order to get the spacing between the columns in the header, then you can set a stylesheet like the following:

      @setStyleSheet("QHeaderView::section:horizontal {margin-right: 2; border: 1px solid}");@

      To get the spacing between the cells, you can subclass your view, call setShowGrid(false), iterate over its columns and rows and draw the grid yourself with the size you need. The example below illustrates this approach:

      @#include <QtGui>

      class TableWidget : public QTableWidget
      {
      Q_OBJECT
      public:
      TableWidget()
      {
      setRowCount(10);
      setColumnCount(5);
      QTableWidgetItem *newItem = new QTableWidgetItem("An item");
      setItem(0,0, newItem);
      setStyleSheet("QHeaderView::section:horizontal {margin-right: 2; border: 1px solid}");
      }

      void paintEvent(QPaintEvent *event)
      {
          QTableWidget::paintEvent(event);
          QPainter painter(viewport());
          int myHeight = horizontalHeader()->height();
          int i = 0; 
          for (i = 0; i < columnCount(); i++) {
              int startPos = horizontalHeader()->sectionViewportPosition(i);
              QPoint myFrom = QPoint(startPos, 0);
              QPoint myTo = QPoint(startPos, height());
              painter.drawLine(myFrom, myTo);
              startPos += horizontalHeader()->sectionSize(i) - 3;
              myFrom = QPoint(startPos, 0);
              myTo = QPoint(startPos, height());
              painter.drawLine(myFrom, myTo);
          }
          for (i = 0; i < rowCount(); i++) { 
              int startPos = verticalHeader()->sectionViewportPosition(i);
              QPoint myFrom = QPoint(0, startPos);
              QPoint myTo = QPoint(width(), startPos);
              painter.drawLine(myFrom, myTo);
              startPos += verticalHeader()->sectionSize(i);
              myFrom = QPoint(0, startPos);
              myTo = QPoint(width(), startPos);
              painter.drawLine(myFrom, myTo);
          }
      }
      

      };

      #include "main.moc"

      int main(int argc, char** argv)
      {
      QApplication app(argc, argv);
      TableWidget table;
      table.setShowGrid(false);
      table.show();
      return app.exec();
      }
      @

      1 Reply Last reply
      0
      • E Offline
        E Offline
        evergreen
        wrote on 16 May 2011, 07:23 last edited by
        #3

        Very usefull
        Thank you

        1 Reply Last reply
        0

        1/3

        12 May 2011, 08:52

        • Login

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