PDA

View Full Version : How to Remove Spacing between widgets added to QHBoxLayout programmetically



vividpath
7th February 2014, 11:25
10024

When using the following code, how can I get rid of the spacing between buttons added to the QHBoxLayout, which is inturn added to QFrame?
Usage of "layout->setSpacing(0)" reduced the space between the buttons, but did not remove the space completely.

Attached is screenshot after executing the below code.

Thanks a lot for your help.



#include <QtGui>

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

QMainWindow* window = new QMainWindow();
QFrame* centralWidget = new QFrame();
QHBoxLayout* layout = new QHBoxLayout();

window->setCentralWidget(centralWidget);
centralWidget->setLayout(layout);

layout->addWidget(new QPushButton("Button 1"));
layout->addWidget(new QPushButton("Button 2"));

window->setWindowTitle("Spacing in HBoxLayout");

layout->addStretch(0);
layout->setContentsMargins(0,0,0,0);
layout->setSpacing(0);

window->show();
return app.exec();
}

vividpath
7th February 2014, 14:57
I found it. Its the border size.
When I edit borders in the css stylesheets, I am able to control the spacing shown in the image.
http://qt-project.org/doc/qt-4.8/stylesheet-customizing.html#box-model