c++qtborderless

How to set central widget fill in the whole main window in Qt


I have a minimal example like this:

mWidget = new QWidget;
vLayout = new QVBoxLayout;
mLabel = new QLabel;
mLabel->setText("Text");
mLabel->setAlignment(Qt::AlignCenter);
mLabel->setStyleSheet("QLabel { border: 2px solid red; }");
this->setCentralWidget(mWidget);
mWidget->setLayout(vLayout);
vLayout->addWidget(mLabel);

enter image description here

As you can see from the above picture, apart from the central widget, there are still some white border outside the red border. What I am trying to do is to let the central widget fill in the whole main window, so there will be no white border outside the red border. Maybe in another way to say is to make a borderless window(do not know if a borderless window is what I am looking for). I googled how to make a borderless window, and I tried setWindowFlags(Qt::FramelessWindowHint), but it is not what I want. I am pretty confused about how to achieve this now? So is the topic How to make a borderless window what I should look into? If it is, does that mean I should invoke some windows api to make that work? Or I should just do it with some other Qt methods?

Hope someone can provide me a way or give me some hints. Thanks.


Solution

  • Why do you use the QMainWindow? It seems that people use that class without thinking what it is for. You only ever want to use QMainWindow if you have toolbars, status bar, or docking sub-windows. Otherwise it's pointless. What you want is the "central widget" be the only widget, without being embedded in the main window. If you've designed it in a .ui file, you'll have to edit it manually to change the class to QWidget, and remove the intervening and now redundant centralWidget completely.