Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QTableWidget: How can I get tighter lines with less vertical spacing padding?

The QTableWdiget is fabulous for simple grid displays. Changing colors, fonts, etc is straightforward.

However, I did not manage to give the grid a 'tighter' look with less vertical whitespace. I see that the Qt documentation talks (eg here) about

  • margin
  • border
  • padding

around widgets, but when I set these I only get changes around the entire grid widget rather than inside.

How can I set this (with a style sheet, or hard-coded options) directly to make the QTableWidget display tighter?

like image 977
Dirk Eddelbuettel Avatar asked Feb 01 '10 18:02

Dirk Eddelbuettel


People also ask

How to adjust margins and spacing between QWidgets in qvboxlayout?

To adjust margins and spacing between QWidgets use the following methods setSpacing and setContentsMargins that are implemented in class QLayout. Example. This code snippet shows how to remove spacing and margins between widgets in instance of QVBoxLayout.

Who can see the vertical header in qtablewidget?

Only users with topic management privileges can see it. I'm using a QTableWidget and I need to show both horizontal and vertical header. If I simply pass a list of strings for vertical header and set it visible it doesn't show.

How to manage the space around an item in qlistwidget?

QListWidget uses an internal model to manage each QListWidgetItem in the list. Spacing property holds the space around the items in the layout. This property is the size of the empty space that is padded around an item in the layout.

How do I change the scroll bar on a qlistwidgetitem?

QListWidget uses an internal model to manage each QListWidgetItem in the list. Setting vertical scroll bar replaces the existing vertical scroll bar with scrollBar, and sets all the former scroll bar’s slider properties on the new scroll bar. The former scroll bar is then deleted.


1 Answers

The code getting 'h' might be unsound. It was just an example. Copy & paste the following rather rudimentary code. Change the value in "setDefaultSectionSize()", recompile, and run. You should see the difference. Setting this to 10 or 50 yields visible results. In the code above, it is possible QFontMetrics or QFont is messing something up.

You can use whatever you want to get the height, but font size makes the most sense.

#include <QtGui>

int main( int argc, char* argv[] )
{
 QApplication app( argc, argv );

 QDialog* my_dialog  = new QDialog();
 QHBoxLayout* layout  = new QHBoxLayout();
 QTableWidget* my_table_widget = new QTableWidget( my_dialog );

 my_table_widget->setRowCount( 10 );
 my_table_widget->setColumnCount( 10 );
 my_table_widget->verticalHeader()->setDefaultSectionSize( 15 );
 layout->addWidget( my_table_widget );
 my_dialog->setLayout( layout );
 my_dialog->resize( 500, 200 );
 my_dialog->show();

 return app.exec();
}

EDIT: I don't know how to format a block of code here... forgive me. :)

Edit 2: I fixed that, and the following simple tighterTable.pro file helps along.

TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .

SOURCES += tighterTable.cpp    # if that is the filename

Thanks a big fat bunch for this. BTW: Editing as code is just indenting by four spaces, and/or hitting the button with the little '101010' in the formatting row.

like image 64
Paul England Avatar answered Sep 20 '22 15:09

Paul England