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)
    {
    	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);
    }
    

  • Lifetime Qt Champion

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