- All (478)
- jom (0)
- Qt Linguist (7)
- Qt Eclipse Integration (9)
- Qt Designer (7)
- Qt Creator (4)
- Qt build system: qmake (31)
- Qt build system: configure (3)
- Qt Assistant (5)
- Printing (4)
- Porting from Qt 3 to Qt 4 (1)
- Plugins (7)
- Qt Visual Studio AddIn (2)
- Qt/MFC Migration (2)
- QtScript (3)
- MDI (2)
- XML (1)
- Widgets (22)
- WebKit (5)
- Tools and Containers (2)
- Threads (2)
- Text Handling (10)
- SQL (6)
- QtTest (1)
- QtService (1)
- Platform: Windows (49)
- Platform: Unix (16)
- Platform: Mac OS X (18)
- Image Formats (2)
- I/O (2)
- Graphicsview (8)
- Font handling (9)
- Event System (18)
- Drag and Drop (4)
- Dialogs (6)
- Desktop integration (3)
- ActiveQt (3)
- Itemviews (60)
- Layout (4)
- Qt Quick (10)
- Qt SDK (1)
- Licensing (2)
- Platform: Embedded Linux (38)
- Painting (32)
- OpenGL (4)
- Object Model (6)
- Network (5)
- Multimedia (3)
- Miscellanous (23)
- Main Window (19)
- Look and Feel (23)
- Development (0)
- Getting Involved (0)
- Routines (0)
How can I get rid of the white space outside the cells of my table?
If you don’t need all of your header sections to be resizable, then you can achieve this by setting the resizeMode [qt.nokia.com] to stretch for one or more of the header sections:
If you do need all of your header sections to be resizeable, then you need to reimplement the sizeHint() [qt.nokia.com] to account for the size of the cells and headers in the table.
Then you need to set this size to be the maximum size of the table. Since you allow resizing of the table’s columns, you also need to connect a slot to the sectionResized [doc.qt.nokia.com] signal and call setMaximumSize(sizeHint()) in there.
The example below demonstrates how this can be done for a toplevel table. If you need this to work for a table that is embedded into another widget, then you need to set the sizePolicy [doc.qt.nokia.com] to fixed, so that the sizeHint() is the only alternative.
- #include <QtGui>
- {
- Q_OBJECT
- public:
- {
- connect(horizontalHeader(), SIGNAL(sectionResized(int, int, int)), this, SLOT(sectionResized()));
- connect(verticalHeader(), SIGNAL(sectionResized(int, int, int)), this, SLOT(sectionResized()));
- }
- {
- int newWidth = 0;
- int newHeight =0;
- for (int i = 0; i < columnCount(); i++) {
- newWidth+= columnWidth(i);
- }
- for (int y = 0; y < rowCount(); y++) {
- newHeight+= rowHeight(y);
- }
- newWidth+=verticalHeader()->width() + 2 *frameWidth();
- newHeight+= horizontalHeader()->height() +2 *frameWidth();
- }
- public slots:
- void sectionResized()
- {
- setMaximumSize(sizeHint());
- }
- };
- #include "main.moc"
- int main(int argc, char **argv)
- {
- TableWidget table(10,10);
- table.setMaximumSize(table.sizeHint());
- table.show();
- table.resize(table.sizeHint());
- return app.exec();
- }

1 comment
December 15, 2010
Lab Rat
I’ve been looking for this for a while now on Google. Qt Dev Net rocks!