为Qt制作无边界窗口 我对QT C++是新手。我下载了最新的windows版本,做了一些教程,非常棒

为Qt制作无边界窗口 我对QT C++是新手。我下载了最新的windows版本,做了一些教程,非常棒,c++,qt,themes,styling,C++,Qt,Themes,Styling,我看到了Qt框架拥有的一些样式选项和它的优点,但现在我需要构建我的应用程序,它的主窗口(窗体)由它设计/使用没有矩形边框(无边框?)的图像进行蒙皮 如何使用Qt实现此功能?如果您正在寻找小部件形状的高级样式,此示例可能会帮助您: 或者,您可能只是在寻找这种标志:Qt::CustomizeWindowint或者简单地Qt::FramelessWindowHint在您的Qt目录中有一个示例应用程序:。我自己也遇到了这个问题,过了一段时间才弄明白。查看Windows和macOS的示例代码 Qt::F

我看到了Qt框架拥有的一些样式选项和它的优点,但现在我需要构建我的应用程序,它的主窗口(窗体)由它设计/使用没有矩形边框(无边框?)的图像进行蒙皮


如何使用Qt实现此功能?

如果您正在寻找小部件形状的高级样式,此示例可能会帮助您:


或者,您可能只是在寻找这种标志:
Qt::CustomizeWindowint
或者简单地
Qt::FramelessWindowHint

在您的Qt目录中有一个示例应用程序:。

我自己也遇到了这个问题,过了一段时间才弄明白。查看Windows和macOS的示例代码


Qt::FramelessWindowHint牺牲了大小调整和最小/最大/关闭,所以这可能不是mot的人们想要的

我创建了一个小示例,介绍如何在Qt5中创建类似VS2013的无框窗口:

您可以在此处获得完整的来源:

另外,这里有一个如何将“主”主窗口嵌入“无框架”窗口的代码概述。您还可以看到如何添加标题栏、按钮以及如何最大化、调整无框窗口的大小和移动

主窗口

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtWidgets>

/*
place your QMainWindow code here
*/
namespace Ui {
  class MainWindow;
}

class MainWindow : public QMainWindow
{
  Q_OBJECT
public:
  explicit MainWindow(QWidget *parent = 0);
  ~MainWindow();
private:
  Ui::MainWindow *ui;
};

/*
this class is to add frameless window supoort and do all the stuff with titlebar and buttons
*/
class BorderlessMainWindow: public QMainWindow
{
  Q_OBJECT
public:
  explicit BorderlessMainWindow(QWidget *parent = 0);
  ~BorderlessMainWindow() {}
protected:
  void mouseMoveEvent(QMouseEvent* event);
  void mousePressEvent(QMouseEvent* event);
  void mouseReleaseEvent(QMouseEvent* event);
  void mouseDoubleClickEvent(QMouseEvent *event);
private slots:
  void slot_minimized();
  void slot_restored();
  void slot_maximized();
  void slot_closed();
private:
  MainWindow *mMainWindow;
  QWidget *mTitlebarWidget;
  QLabel *mWindowTitle;
  QPushButton *mMinimizeButton;
  QPushButton *mRestoreButton;
  QPushButton *mMaximizeButton;
  QPushButton *mCloseButton;
  QPoint mLastMousePosition;
  bool mMoving;
  bool mMaximized;
};

#endif // MAINWINDOW_H
\ifndef主窗口
#定义主窗口
#包括
/*
将QMainWindow代码放在此处
*/
名称空间用户界面{
类主窗口;
}
类主窗口:公共QMainWindow
{
Q_对象
公众:
显式主窗口(QWidget*parent=0);
~main窗口();
私人:
Ui::MainWindow*Ui;
};
/*
这个类将添加无框窗口支持,并使用标题栏和按钮完成所有工作
*/
类BorderlessMainWindow:公共QMainWindow
{
Q_对象
公众:
显式无边框窗口(QWidget*parent=0);
~BorderlessMainWindow(){}
受保护的:
作废mouseMoveEvent(QMouseEvent*事件);
作废鼠标压力事件(QMouseEvent*事件);
无效mouseReleaseEvent(QMouseEvent*事件);
void mouseDoubleClickEvent(QMouseEvent*事件);
专用插槽:
空槽_最小化();
空槽_已恢复();
空槽_最大化();
空槽_闭合();
私人:
主窗口*主窗口;
QWidget*mTitlebarWidget;
QLabel*mWindowTitle;
QPushButton*mMinimizeButton;
QPushButton*mRestoreButton;
QPushButton*mMaximizeButton;
QPushButton*mCloseButton;
QPoint-mLastMousePosition;
布尔移动;
布尔最大化;
};
#endif//main窗口
mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

/*
frameless window class: it adds the MainWindow class inside the centralWidget
*/
BorderlessMainWindow::BorderlessMainWindow(QWidget *parent) : QMainWindow(parent, Qt::CustomizeWindowHint ) {
  setObjectName("borderlessMainWindow");
  setWindowFlags(Qt::FramelessWindowHint| Qt::WindowSystemMenuHint);
  // to fix taskbar minimize feature
  setWindowFlags(windowFlags() | Qt::WindowMinimizeButtonHint);

  mMainWindow = new MainWindow(this);
  setWindowTitle(mMainWindow->windowTitle());

  QVBoxLayout *verticalLayout = new QVBoxLayout();
  verticalLayout->setSpacing(0);
  verticalLayout->setMargin(1);

  QHBoxLayout *horizontalLayout = new QHBoxLayout();
  horizontalLayout->setSpacing(0);
  horizontalLayout->setMargin(0);

  mTitlebarWidget = new QWidget(this);
  mTitlebarWidget->setObjectName("titlebarWidget");
  mTitlebarWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
  mTitlebarWidget->setLayout(horizontalLayout);

  mMinimizeButton = new QPushButton(mTitlebarWidget);
  mMinimizeButton->setObjectName("minimizeButton");
  connect(mMinimizeButton, SIGNAL(clicked()), this, SLOT(slot_minimized()));

  mRestoreButton = new QPushButton(mTitlebarWidget);
  mRestoreButton->setObjectName("restoreButton");
  mRestoreButton->setVisible(false);
  connect(mRestoreButton, SIGNAL(clicked()), this, SLOT(slot_restored()));

  mMaximizeButton = new QPushButton(mTitlebarWidget);
  mMaximizeButton->setObjectName("maximizeButton");
  connect(mMaximizeButton, SIGNAL(clicked()), this, SLOT(slot_maximized()));

  mCloseButton = new QPushButton(mTitlebarWidget);
  mCloseButton->setObjectName("closeButton");
  connect(mCloseButton, SIGNAL(clicked()), this, SLOT(slot_closed()));

  mWindowTitle = new QLabel(mTitlebarWidget);
  mWindowTitle->setObjectName("windowTitle");
  mWindowTitle->setText(windowTitle());

  horizontalLayout->addWidget(mWindowTitle);
  horizontalLayout->addStretch(1);
  horizontalLayout->addWidget(mMinimizeButton);
  horizontalLayout->addWidget(mRestoreButton);
  horizontalLayout->addWidget(mMaximizeButton);
  horizontalLayout->addWidget(mCloseButton);

  verticalLayout->addWidget(mTitlebarWidget);
  verticalLayout->addWidget(mMainWindow);

  QWidget *centralWidget = new QWidget(this);
  centralWidget->setObjectName("centralWidget");
  centralWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
  centralWidget->setLayout(verticalLayout);

  setCentralWidget(centralWidget);
}
void BorderlessMainWindow::mousePressEvent(QMouseEvent* event) {
  if (!mTitlebarWidget->underMouse() && !mWindowTitle->underMouse())
    return;

  if(event->button() == Qt::LeftButton) {
    mMoving = true;
    mLastMousePosition = event->pos();
  }
}
void BorderlessMainWindow::mouseMoveEvent(QMouseEvent* event) {
  if (!mTitlebarWidget->underMouse() && !mWindowTitle->underMouse())
    return;

  if( event->buttons().testFlag(Qt::LeftButton) && mMoving) {
    this->move(this->pos() + (event->pos() - mLastMousePosition));
  }
}
void BorderlessMainWindow::mouseReleaseEvent(QMouseEvent* event) {
  if (!mTitlebarWidget->underMouse() && !mWindowTitle->underMouse())
    return;

  if(event->button() == Qt::LeftButton) {
    mMoving = false;
  }
}
void BorderlessMainWindow::mouseDoubleClickEvent(QMouseEvent *event) {
  Q_UNUSED(event);
  if (!mTitlebarWidget->underMouse() && !mWindowTitle->underMouse())
    return;

  mMaximized = !mMaximized;
  if (mMaximized) {
    slot_maximized();
  } else {
    slot_restored();
  }
}
void BorderlessMainWindow::slot_minimized() {
  setWindowState(Qt::WindowMinimized);
}
void BorderlessMainWindow::slot_restored() {
  mRestoreButton->setVisible(false);
  mMaximizeButton->setVisible(true);
  setWindowState(Qt::WindowNoState);
  setStyleSheet("#borderlessMainWindow{border:1px solid palette(highlight);}");
}
void BorderlessMainWindow::slot_maximized() {
  mRestoreButton->setVisible(true);
  mMaximizeButton->setVisible(false);
  setWindowState(Qt::WindowMaximized);
  setStyleSheet("#borderlessMainWindow{border:1px solid palette(base);}");
}
void BorderlessMainWindow::slot_closed() {
  close();
}

/*
MainWindow class: put all your code here
*/
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent, Qt::FramelessWindowHint), ui(new Ui::MainWindow) {
  ui->setupUi(this);
  statusBar()->setSizeGripEnabled(true);
}

MainWindow::~MainWindow() {
  delete ui;
}
#包括“mainwindow.h”
#包括“ui_main window.h”
#包括
/*
无框架窗口类:它在centralWidget中添加MainWindow类
*/
BorderlessMainWindow::BorderlessMainWindow(QWidget*parent):QMainWindow(parent,Qt::CustomizeWindowint){
setObjectName(“无边框窗口”);
setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint);
//修复任务栏最小化功能
setWindowFlags(windowFlags()| Qt::WindowMinimizeButtonHint);
mMainWindow=新的主窗口(此窗口);
setWindowTitle(mMainWindow->windowTitle());
QVBoxLayout*垂直布局=新的QVBoxLayout();
垂直布局->设置间距(0);
垂直布局->设置边距(1);
QHBoxLayout*水平布局=新的QHBoxLayout();
水平布局->设置间距(0);
水平布局->设置边距(0);
mTitlebarWidget=新的QWidget(此);
mTitlebarWidget->setObjectName(“标题栏小部件”);
MTITLEBRWidget->setSizePolicy(QSizePolicy::Expansing,QSizePolicy::Preferred);
mTitlebarWidget->setLayout(水平布局);
mMinimizeButton=新的QPushButton(mTitlebarWidget);
mMinimizeButton->setObjectName(“minimizeButton”);
连接(mMinimizeButton,SIGNAL(clicked()),此,插槽(SLOT_minimized());
mRestoreButton=新的QPushButton(mTitlebarWidget);
mRestoreButton->setObjectName(“restoreButton”);
mRestoreButton->setVisible(假);
连接(mRestoreButton,信号(单击()),此,插槽(插槽恢复());
mMaximizeButton=新的QPushButton(mTitlebarWidget);
mMaximizeButton->setObjectName(“maximizeButton”);
连接(mMaximizeButton,SIGNAL(clicked()),此,插槽(SLOT_maximized());
mCloseButton=新的QPushButton(mTitlebarWidget);
mCloseButton->setObjectName(“closeButton”);
连接(mCloseButton,信号(clicked()),此,插槽(SLOT_closed());
mWindowTitle=新的QLabel(mTitlebarWidget);
mWindowTitle->setObjectName(“windowTitle”);
mWindowTitle->setText(windowTitle());
水平布局->添加小部件(mWindowTitle);
水平布置->添加拉伸(1);
水平布局->添加小部件(mMinimizeButton);
水平布局->添加小部件(mRestoreButton);
水平布局->添加小部件(mMaximizeButton);
水平布局->添加小部件(MCLOSE按钮);
垂直布局->添加小部件(mTitlebarWidget);
垂直布局->添加小部件(mMainWindow);
QWidget*centralWidget=新的QWidget(此);
centralWidget->setObjectName(“centralWidget”);
centralWidget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
centralWidget->setLayout(垂直布局);
setCentralWidget(centralWidget);
}
void BorderlessMainWindow::MousePresseEvent(QMouseEvent*事件){
如果(!mtitlebridget->underMouse()&&!mWindowTitle->underMouse())
返回;
如果(事件->按钮()==Qt::LeftButton){
M移动=真;
mLastMousePosition=事件->位置();
}
}
void BorderlessMainWindow::mouseMoveEvent(QMouseEvent*事件){
如果(!mtitlebridget->underMouse()&&!mWindowTitle->underMouse())
返回;
if(event->buttons().testFlag(Qt::LeftButton)&&mMoving){
this->move(this->pos()+(事件->pos()-mLastMousePosition));
}
}
void BorderlessMainWindow::mouseReleaseEvent(QMouseEvent*event){
如果(!mtitlebridget->underMouse()&&!mWindowTitle->underMouse())
返回;
如果(事件->按钮()==Qt::LeftButton){
M移动=错误;
}
}
void BorderlessMainWindow::mouseDoubleClickEvent(QMouseEvent*事件){
Q_未使用(事件);
如果(!mtitlebridget->underMouse()&&!mWindowTitle->underMouse())
返回;
mMaximized=!mMaximized;
如果(最大化){
槽_最大化();
}埃尔