设置无边窗属性
MainWidget::MainWidget(QWidget* parent)
: QWidget(parent) {
m_nBorderWidth = 5;
//this->setWindowFlags(Qt::FramelessWindowHint |Qt::WindowMinMaxButtonsHint );
this->setWindowFlags(Qt::FramelessWindowHint);
initUI();
}
使用this->setWindowFlags(Qt::FramelessWindowHint);
这行代码设置无边窗属性.
但是我们不设置Qt::WindowMinMaxButtonsHint
这个属性,如果我们设置了这个属性会导致无法使用window api实现窗口的缩放.
自定义标题栏
添加新建类作为我们的自定义标题栏.
CTitileBar.h 头文件
#pragma once
#include <QWidget>
#include <QLabel>
#include <QPushButton>
class CTitleBar : public QWidget
{
Q_OBJECT
public:
CTitleBar(QWidget* p = nullptr);
~CTitleBar();
private:
void initUI();
private:
void mousePressEvent(QMouseEvent* event) override;
private:
QLabel* Label_mpLogo;
QLabel* Label_mpTitleText;
QPushButton* Btn_mpSet;
QPushButton* Btn_mpMin;
QPushButton* Btn_mpMax;
QPushButton* Btn_mpClose;
};
void initUI();
定义初始化函数用来初始化该类,即自定义标题栏.
void mousePressEvent(QMouseEvent* event) override;
重载mousePressEvent
函数用来让窗口可以拖动.
对鼠标按下事件进行重写.
这些私有成员是该标题栏拥有的控件.
CTitileBar.cpp 源文件
#include "CTitleBar.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <qt_windows.h>
CTitleBar::CTitleBar(QWidget* p)
:QWidget(p)
{
initUI();
}
CTitleBar::~CTitleBar() {
}
void CTitleBar::initUI() {
setAttribute(Qt::WA_StyledBackground);
this->setStyleSheet("background-color:rgb(156,156,156)");
this->setFixedHeight(32);
Label_mpLogo = new QLabel(this);
Label_mpTitleText = new QLabel(this);
Btn_mpSet = new QPushButton(this);
Btn_mpMin = new QPushButton(this);
Btn_mpMax = new QPushButton(this);
Btn_mpClose = new QPushButton(this);
Label_mpLogo->setFixedSize(24, 24);
Label_mpTitleText->setText("我是标题");
Label_mpTitleText->setFixedWidth(120);
Btn_mpSet->setFixedSize(24, 24);
Btn_mpMin->setFixedSize(24, 24);
Btn_mpMax->setFixedSize(24, 24);
Btn_mpClose->setFixedSize(24, 24);<