Remove all spacing in QGridLayout

I am trying to build a programmaticaly (with Qt 4.6) window containing the QPushButton series, all packed together. It should look like this (I call the toolbar):

Toolbar image http://img99.imageshack.us/img99/9853/examplezk.png

So, I created a Toolbox class derived from QWidget, which has the following constructor:

 Toolbox::Toolbox (void) : QWidget (0, Qt::Tool) { setWindowTitle (tr ("Toolbox")); QGridLayout *group = new QGridLayout (this); group->setSpacing (0); group->setContentsMargins (0, 0, 0, 0); group->setSizeConstraint (QLayout::SetFixedSize); setLayout (group); unsigned k = 0; QPushButton *buttons = new QPushButton[6]; for (unsigned i = 0; i < 3; i++) for (unsigned j = 0; j < 2; j++) { buttons[k].setIcon (QIcon ("test.png")); buttons[k].setIconSize (QSize (32, 32)); buttons[k].setContentsMargins (0, 0, 0, 0); buttons[k].setCheckable (true); buttons[k].setAutoExclusive (true); group->addWidget (&buttons[k], i, j); k++; } buttons[1].setChecked (true); 

Somehow this does not work, and my buttons do not pack together:

result http://img9.imageshack.us/img9/774/resultr.png

I will not be able to remove this vertical span (and the fields surrounding the entire array). Any help is appreciated.

+7
qt macos
source share
3 answers

This seems to be considered normal: see the corresponding bug report that has been closed. The reported workaround doesn't seem to work for me.

+1
source share

Since you set the size limit in the layout to QLayout::SetFixedSize , Qt will use the widget size hint as a fixed size. You may need to redefine QWidget::sizeHint() in the Toolbox class so that the widget is as large as necessary to fit all buttons (for six buttons, the width will be 64 and the height will be 96).

0
source share

If you use the plastique style, which is now standard in Qt4.6, the borders of the QPushButtons are displayed inside the widget. Try using one of the other styles. eg:.

 #include <QGtkStyle> QApplication a(argc, argv, true); a.setStyle("gtk"); 

The style can also be set in a separate widget using the QWidget :: setStyle () function.

0
source share

All Articles