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

标签 c++ windows qt maximize-window

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

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

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

Windows 标题: enter image description here

没有 Windows 标题: enter image description here

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

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

示例窗口.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_

示例窗口.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();
}

最佳答案

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

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

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

mainWindow.showFullScreen();

setWindowState(Qt::WindowFullScreen);

更新: 除了您的解决方案外,我还找到了另一个解决方案:

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

关于c++ - 最大化窗口维护任务栏限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39816031/

相关文章:

c++ - union 体中指针和数组的不同地址

c++ - 迭代器上的 vector 下标超出范围

c++ - 写入后跨进程读取文件一致性

c++ - 解决控制台关闭问题的最佳实践是什么?

windows - 在批处理脚本中循环遍历文件名

c++ - QT 重绘/重绘/更新/做某事

c++ - MP3 文件可视化

c++ - Release模式下的 OpenGL 粉红色

c++ - C++ 和 QML 中的 QT QML 项

c++ - QFile 类嵌套的问题