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 Updated to NodeBB v4.3 + New Features

QTableWidget spaces between columns

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 13.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.
  • E Offline
    E Offline
    evergreen
    wrote on 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 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 last edited by
        #3

        Very usefull
        Thank you

        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