Maximize window maintaining taskbar limits

て烟熏妆下的殇ゞ 提交于 2020-02-04 04:39:25

问题


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:

Without Windows title:

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();
}

回答1:


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());



回答2:


I think that I've found a solution by using this slot when maximize button is clicked:

void SampleWindow::maximize() {
  //setWindowState(Qt::WindowFullScreen);
  QDesktopWidget *desktop = QApplication::desktop();
  QRect desktopGeometry = desktop->availableGeometry();
  int desktopHeight = desktopGeometry.height();
  int desktopWidth = desktopGeometry.width();
  int padx = (frameGeometry().width() - geometry().width()) / 2;
  setFixedSize(desktopWidth, desktopHeight);
  move(-padx,0);
}

I need to test it more but at the moment the area seems correct.



来源:https://stackoverflow.com/questions/39816031/maximize-window-maintaining-taskbar-limits

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!