Qt C++ 实现无边框窗口
// widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QDebug>
#include <QHBoxLayout>
#include <QMouseEvent>
#include <QPushButton>
#include <QString>
#include <QWidget>
#define PADDING 6
enum Location {
TOP,
BOTTOM,
LEFT,
RIGHT,
TOP_LEFT,
TOP_RIGHT,
BOTTOM_LEFT,
BOTTOM_RIGHT,
CENTER
};
class Widget : public QWidget {
Q_OBJECT
public:
Widget(QWidget* parent = nullptr);
~Widget();
protected:
void mousePressEvent(QMouseEvent* event);
void mouseMoveEvent(QMouseEvent* event);
void mouseReleaseEvent(QMouseEvent* event);
private:
void setCursorShape(const QPoint& point);
private:
// 左键有没有按下
bool isLeftPressed;
// 鼠标按下的位置 偏移
QPoint mouseOffset;
Location location;
};
#endif // WIDGET_H
// widget.cpp
#include "widget.h"
Widget::Widget(QWidget* parent) : QWidget(parent) {
// 设置窗口的宽高
this->setMinimumWidth(500);
this->setMinimumHeight(300);
// 设置背景色
this->setStyleSheet("background: #303030");
// 添加两个按钮 - 水平布局
QHBoxLayout* layout = new QHBoxLayout(this);
layout->setSpacing(10);
layout->setContentsMargins(10, 10, 10, 10);
QPushButton* btn1 = new QPushButton("确定");
QPushButton* btn2 &#