PDA

View Full Version : QGridLayout and QWidget::setContentsMargins



fross
13th January 2011, 20:56
Using Qt 4.7.1 I'm trying to place a QCheckbox under a QLineEdit using a QGridLayout.

I don't want the QCheckBox to be aligned with the QLineEdit. I want it to be offset by 20 pixels. In order to do so I use setContentsMargins on the checkbox but it doesn't seem to work. How come?

http://i.imgur.com/A2Ci2.png (http://imgur.com/A2Ci2)


#include <QtGui>

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

QLabel* label = new QLabel("Label: ");
QLineEdit* lineEdit = new QLineEdit;
QCheckBox* checkBox = new QCheckBox("Checkbox");

// Layout.
QGridLayout* layout = new QGridLayout;
layout->setMargin(0);

// First row.
layout->addWidget(label, 0, 0);
layout->addWidget(lineEdit, 0, 1);

// Second row.
checkBox->setContentsMargins(20, 0, 0, 0);
layout->addWidget(checkBox, 2, 1, Qt::AlignLeft);

QWidget* widget = new QWidget;
widget->setLayout(layout);

QMainWindow mainWindow;
mainWindow.setCentralWidget(widget);
mainWindow.setWindowTitle("Demo");
mainWindow.adjustSize();
mainWindow.show();

return app.exec();
}

MattPhillips
14th January 2011, 02:52
This doesn't address your question directly but could you also just stick in a spacer and adjust your proportions to get the offset you want?