Important: Please read the Qt Code of Conduct -

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

  • 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)
    	QTableWidget *table = new QTableWidget;
    	QFont font("Tahoma", 11);
    	// doesn't make any difference
    	QHeaderView *rowSize = table->verticalHeader();
    	rowSize->setSectionResizeMode(QHeaderView::Interactive);	// Fixed
    	for(int i = 0; i < 10; 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);

  • Lifetime Qt Champion

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

  • @mpergand 'setMinimumSectionSize' is necessary.

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