9

I have a QMainWindow and I have inserted mdiArea in the main window.
You must first see the following image:

As you seen the gray area is mdiArea and the spaces(margin) that between main window and mdiArea are what I want to remove it.
I have used setContentsMargins function, but does not do anything.

How can I remove these spaces ?

I want to be like the following image: enter image description here

1 Answer 1

21

QMainWindow has a central widget that has a layout. The layout you use on QMainWindow is should be used upon its central widget instead. Hence you have to call

QWidget* QMainWindow::centralWidget() first,

so you can get the central widget first and then use

void QLayout::setContentsMargins(int left, int top, int right, int bottom)

to adjust its layout. The diagram below comes from Qt's documentation.

enter image description here enter image description here

Creating a main window without a central widget is not supported. You must have a central widget even if it's just a placeholder.


For example, you could have the following in QMainWindow's constructor:

centralWidget()->layout()->setContentsMargins(0, 0, 0, 0);
statusBar()->hide();
ui->mainToolBar->hide();

enter image description here

The status bar and the tool bar have been hidden, in order to remove as much blank space as possible.

5
  • I have wrote this code in the constructor of MainWindow. but unfortunately, still does not want to remove the spaces.
    – Lion King
    Jun 16, 2014 at 9:06
  • @LionKing What do you mean "doesn't want to remove the spaces"? Isn't that what you want?
    – Tay2510
    Jun 16, 2014 at 9:08
  • @LionKing OK I've edited my post, need to call centralWidget first and get it's layout.
    – Tay2510
    Jun 16, 2014 at 9:14
  • Yes, that's it. but can you please what the mean of central widget?
    – Lion King
    Jun 16, 2014 at 9:18
  • @LionKing It's the base widget of QMainWindow, I'll update the post and provide detail in a minute.
    – Tay2510
    Jun 16, 2014 at 9:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.