最大化窗口维护任务栏限制

时间:2016-10-02 10:26:01

标签: c++ windows qt maximize-window

我想创建一个没有Windows标题栏的Qt应用程序(我想创建一个自定义栏)。

我创建了三个按钮,用于最小化,最大化和关闭窗口。一切正常,除了考虑到当我最大化窗口时,应用程序不考虑任务栏,并且最大化窗口占据整个屏幕,位于任务栏下方。来自Windows的普通最大化命令可以最大化应用程序窗口,避免进入任务栏。

如果我不使用Qt::CustomizeWindowHint,则会出现窗口标题栏,并且最大化行为是正确的;但如果我使用这个标志,标题栏就会消失,应用程序也会在窗口下:在这里你可以找到两个解释行为的截图:

使用Windows标题: enter image description here

没有Windows标题: enter image description here

正如您在后一种情况下所见,“关闭”按钮进入任务栏,因为应用程序占据了整个屏幕。

如何在不使用Windows标题栏的情况下避免此行为?我想重新创建与窗口标题栏相同的行为。

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

2 个答案:

答案 0 :(得分:2)

此行为取决于系统。我在Windows 7和Linux Mint KDE上测试了您的代码,行为也不同。在Windows 7中,任务栏已隐藏,窗口中填充了任务栏区域。在KDE中,我注意到窗口正确地最大化(避免小部件面板而不是隐藏它们)。

但是当我尝试在兼容模式的Windows 10中运行代码时,我只能在与Windows Vista和旧版本兼容的情况下重复Win7的行为。

对于Windows 10,我找到了另一种解决方案:如果适合您,可以全屏显示窗口:

mainWindow.showFullScreen();

setWindowState(Qt::WindowFullScreen);

<强> UPD : 除了你的解决方案,我发现了另一个:

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

答案 1 :(得分:1)

我认为在点击最大化按钮时使用此插槽找到了解决方案:

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

我需要对它进行更多测试,但此刻该区域看似正确。

相关问题