Results 1 to 2 of 2

Thread: QGridLayout and QWidget::setContentsMargins

  1. #1
    Join Date
    Jan 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QGridLayout and QWidget::setContentsMargins

    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?



    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication app(argc, argv);
    6.  
    7. QLabel* label = new QLabel("Label: ");
    8. QLineEdit* lineEdit = new QLineEdit;
    9. QCheckBox* checkBox = new QCheckBox("Checkbox");
    10.  
    11. // Layout.
    12. QGridLayout* layout = new QGridLayout;
    13. layout->setMargin(0);
    14.  
    15. // First row.
    16. layout->addWidget(label, 0, 0);
    17. layout->addWidget(lineEdit, 0, 1);
    18.  
    19. // Second row.
    20. checkBox->setContentsMargins(20, 0, 0, 0);
    21. layout->addWidget(checkBox, 2, 1, Qt::AlignLeft);
    22.  
    23. QWidget* widget = new QWidget;
    24. widget->setLayout(layout);
    25.  
    26. QMainWindow mainWindow;
    27. mainWindow.setCentralWidget(widget);
    28. mainWindow.setWindowTitle("Demo");
    29. mainWindow.adjustSize();
    30. mainWindow.show();
    31.  
    32. return app.exec();
    33. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Aug 2009
    Posts
    140
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    22

    Default Re: QGridLayout and QWidget::setContentsMargins

    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?

Similar Threads

  1. QGridLayout: Getting the list of QWidget added
    By ShaChris23 in forum Qt Programming
    Replies: 2
    Last Post: 12th November 2010, 09:47
  2. Set size when insert a QWidget into a QGridLayout
    By Tondog in forum Qt Programming
    Replies: 3
    Last Post: 4th June 2010, 07:35
  3. Replies: 0
    Last Post: 9th March 2010, 09:44
  4. QMainWindow and setContentsMargins
    By themolecule in forum Qt Programming
    Replies: 0
    Last Post: 7th February 2010, 20:03
  5. QToolbar setContentsMargins
    By rshabsin in forum Qt Programming
    Replies: 2
    Last Post: 18th September 2007, 23:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.