Search code examples
c++windowsqtmaximize-window

Maximize window maintaining taskbar limits


I want to create a Qt application without the windows title bar (I want to create a customized one).

I've created three buttons for minimizing, maximizing and closing the window. Everything works except for considering that when I maximize the window, application doesn't take into account the taskbar, and the maximized window takes the entire screen, going under the taskbar. A normal maximize command from windows instead maximizes the application window avoiding to go under the taskbar.

If I don't use the Qt::CustomizeWindowHint the window title bar appears, and maximizing behaviour is correct; but if I use this flag, the title bar disappears and the application goes under the window: here you can find two screenshots explaning the behaviour:

With Windows title: enter image description here

Without Windows title: enter image description here

As you can see in latter case che "Close" button goes inside the taskbar because the application takes the entire screen.

How can I avoid this behaviour without using windows title bar? I want to recreate the same behaviour as with the window title bar.

SampleWindow.h

#ifndef SAMPLEWINDOW_H_
#define SAMPLEWINDOW_H_

#include <QMainWindow>
#include <QPushButton>
#include <QHBoxLayout>

class SampleWindow : public QMainWindow {

  Q_OBJECT

public:
  SampleWindow();
  virtual ~SampleWindow() = default;
};

#endif // !SAMPLEWINDOW_H_

SampleWindow.cpp

#include "SampleWindow.h"
#include <QCoreApplication>

SampleWindow::SampleWindow() :
QMainWindow() {
  // With uncommenting this line the title bar disappears
  // but application goes under the taskbar when maximized
  //
  //setWindowFlags(Qt::CustomizeWindowHint);
  auto centralWidget = new QWidget(this);
  auto layout = new QHBoxLayout(this);
  auto minimizeButton = new QPushButton("Minimize", this);
  auto maximizeButton = new QPushButton("Maximize", this);
  auto closeButton = new QPushButton("Close", this);
  layout->addWidget(minimizeButton);
  layout->addWidget(maximizeButton);
  layout->addWidget(closeButton);
  centralWidget->setLayout(layout);
  setCentralWidget(centralWidget);
  connect(closeButton, &QPushButton::clicked, [=]() {QCoreApplication::quit();});
  connect(minimizeButton, &QPushButton::clicked, this, [=]() {setWindowState(Qt::WindowMinimized);});
  connect(maximizeButton, &QPushButton::clicked, this, [=]() {setWindowState(Qt::WindowMaximized);});
}

Main.cpp

#include <QApplication>
#include "SampleWindow.h"

int main(int argc, char* argv[]) {
  QApplication app(argc, argv);
  SampleWindow mainWindow;
  mainWindow.show();
  return app.exec();
}

Solution

  • This behavior depends on system. I tested your code on Windows 7 and Linux Mint KDE and behavior was different. In Windows 7 taskbar has hidden and window has filled area of taskbar. In KDE I have noticed that window maximizes correctly (avoids widget panels and not hides them).

    However when I try to run code in Windows 10 with compatibility mode, I was able to repeat behavior of Win7 only in compatibility with Windows Vista and older versions.

    For Windows 10 I found another solution: you can maximize your window in fullscreen if that suits you:

    mainWindow.showFullScreen();
    

    or

    setWindowState(Qt::WindowFullScreen);
    

    UPD: In addition to your solution I found another one:

    setGeometry(QApplication::desktop()->availableGeometry().x(),
                QApplication::desktop()->availableGeometry().y(),
                QApplication::desktop()->availableGeometry().width(),
                QApplication::desktop()->availableGeometry().height());