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. How to tighten up (reduce) the row height in a table
Forum Updated to NodeBB v4.3 + New Features

How to tighten up (reduce) the row height in a table

Scheduled Pinned Locked Moved Solved General and Desktop
qt creatortable widget
5 Posts 3 Posters 3.0k Views 3 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.
  • SprezzaturaS Offline
    SprezzaturaS Offline
    Sprezzatura
    wrote on last edited by
    #1

    The default row height is too high, it leaves too much space between lines.

    setRowHeight is not working if the value is less than the default. How do I reduce the line spacing?

    #include "TableRowHeight.h"
    #include <qtablewidget.h>
    #include <QHeaderView>
    #include <QGridLayout>
    
    TableRowHeight::TableRowHeight(QWidget *parent) : QMainWindow(parent)
    {
    	ui.setupUi(this);
    
    	QTableWidget *table = new QTableWidget;
    	QFont font("Tahoma", 11);
    
    	table->setFont(font);
    	table->setColumnCount(1);
    
    	// doesn't make any difference
    	QHeaderView *rowSize = table->verticalHeader();
    	rowSize->setSectionResizeMode(QHeaderView::Interactive);	// Fixed
    	rowSize->setDefaultSectionSize(5);
    
    	for(int i = 0; i < 10; i++)
    	{
    		table->insertRow(i);
    		int rh = table->rowHeight(i);
    		int targetH = rh * 0.5;
    		table->setRowHeight(i, targetH);  // no effect if reducing line height
    		int hAfter = table->rowHeight(i);
    
    		QTableWidgetItem *item;
    		char wk[128];
    		_snprintf(wk, sizeof(wk), "%d height before{%d} setHeight{%d} height after{%d}", i, rh, targetH, hAfter);
    		item = new QTableWidgetItem(wk);
    		table->setItem(i, 0, item);
    	}
    	this->setCentralWidget(table);
    }
    
    1 Reply Last reply
    1
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      Did you try setting the minimum ?
      setMinimumSectionSize(int size) and then use setRowHeight

      SprezzaturaS 1 Reply Last reply
      4
      • M Offline
        M Offline
        mpergand
        wrote on last edited by mpergand
        #3

        See How set height of all rows

        SprezzaturaS 1 Reply Last reply
        3
        • M mpergand

          See How set height of all rows

          SprezzaturaS Offline
          SprezzaturaS Offline
          Sprezzatura
          wrote on last edited by
          #4

          @mpergand 'setMinimumSectionSize' is necessary.

          1 Reply Last reply
          1
          • mrjjM mrjj

            Hi
            Did you try setting the minimum ?
            setMinimumSectionSize(int size) and then use setRowHeight

            SprezzaturaS Offline
            SprezzaturaS Offline
            Sprezzatura
            wrote on last edited by
            #5

            @mrjj Excellent! setMinimumSectionSize did the trick. setRowHeight does not seem necessary.

            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