Qt自定义窗口基类

VirtualMoveWidget.h

#ifndef VIRTUALMOVEWIDGET_H
#define VIRTUALMOVEWIDGET_H

#include <QLabel>
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QMouseEvent>

class HeadWidget:public QWidget
{
    Q_OBJECT
private:
    QPoint last;
    QLabel* titleIcon;
    QLabel* titleText;
    QPushButton* btnMin;
    QPushButton* btnMax;
    QPushButton* btnClose;
    QHBoxLayout* mainLayout;
public:
    explicit HeadWidget(QWidget *parent = nullptr);
    ~HeadWidget(){};
protected:
    virtual void mousePressEvent(QMouseEvent*);
    virtual void mouseMoveEvent(QMouseEvent*);
    virtual void mouseReleaseEvent(QMouseEvent*);
signals:
    void MaxWidget();
    void HideWidget();
    void CloseWidget();
    void MoveWinPoint(int, int);
};

class VirtualMoveWidget : public QWidget
{
    Q_OBJECT
private:
    HeadWidget *headWidget;
    QWidget* bodyWidget;
    QVBoxLayout* mainLayout;
public:
    explicit VirtualMoveWidget(QWidget *parent = nullptr);
    virtual ~VirtualMoveWidget()=0;
public:
    void SetHeadWidth(int);
signals:
};

#endif // VIRTUALMOVEWIDGET_H

VirtualMoveWidget.cpp

#include "VirtualMoveWidget.h"

HeadWidget::HeadWidget(QWidget *parent)
    : QWidget(parent)
{
    titleIcon=new QLabel;
    titleText=new QLabel;
    btnMin=new QPushButton;
    btnMax=new QPushButton;
    btnClose=new QPushButton;
    mainLayout=new QHBoxLayout;

    titleIcon->setObjectName("titleIcon");
    titleText->setObjectName("titleText");
    btnMin->setObjectName("btnMin");
    btnMax->setObjectName("btnMax");
    btnClose->setObjectName("btnClose");
    this->setObjectName("HeadWidget");

    titleIcon->setFixedSize(25,25);
    titleText->setFixedHeight(25);
    btnMin->setFixedSize(25,25);
    btnMax->setFixedSize(25,25);
    btnClose->setFixedSize(25,25);

    mainLayout->setMargin(5);
    mainLayout->setSpacing(10);
    mainLayout->addWidget(titleIcon);
    mainLayout->addWidget(titleText);
    mainLayout->addStretch(1);
    mainLayout->addWidget(btnMin);
    mainLayout->addWidget(btnMax);
    mainLayout->addWidget(btnClose);
    setLayout(mainLayout);

    connect(btnMin, &QPushButton::clicked, this, [=] (bool){emit HideWidget(); });
    connect(btnMax,&QPushButton::clicked,this,[=](bool){emit MaxWidget();});
    connect(btnClose, &QPushButton::clicked, this, [=] (bool){emit CloseWidget(); });
}
void HeadWidget::mousePressEvent(QMouseEvent *e)
{
    last = e->globalPos();
}

void HeadWidget::mouseMoveEvent(QMouseEvent *e)
{
    if ((last.x() == 0) && (last.y() == 0))
        return;
    int dx = e->globalX() - last.x();
    int dy = e->globalY() - last.y();
    last = e->globalPos();
    emit MoveWinPoint(x() + dx, y() + dy);
}

void HeadWidget::mouseReleaseEvent(QMouseEvent *e)
{
    if ((last.x() == 0) && (last.y() == 0))
        return;
    int dx = e->globalX() - last.x();
    int dy = e->globalY() - last.y();
    emit MoveWinPoint(x() + dx, y() + dy);
}


VirtualMoveWidget::VirtualMoveWidget(QWidget *parent)
    : QWidget(parent)
{
    headWidget=new HeadWidget;
    bodyWidget=new QWidget;
    mainLayout=new QVBoxLayout;
    headWidget->setFixedHeight(30);

    mainLayout->setMargin(0);
    mainLayout->setSpacing(0);
    mainLayout->addWidget(headWidget);
    mainLayout->addWidget(bodyWidget);
    setLayout(mainLayout);

    connect(headWidget, &HeadWidget::HideWidget, this, [=] {this->hide(); });
    connect(headWidget, &HeadWidget::CloseWidget, this, [=] {this->close(); });
    connect(headWidget, &HeadWidget::MaxWidget, this, [=] {this->showMaximized(); });
    connect(headWidget, &HeadWidget::MoveWinPoint, this, [=](int x, int y) {move(this->x() + x, this->y() + y); });
}

VirtualMoveWidget::~VirtualMoveWidget()
{
    if(headWidget!=Q_NULLPTR){
        headWidget->deleteLater();
        headWidget=Q_NULLPTR;
    }
    if(bodyWidget!=Q_NULLPTR){
        bodyWidget->deleteLater();
        bodyWidget=Q_NULLPTR;
    }
    if(mainLayout!=Q_NULLPTR){
        mainLayout->deleteLater();
        mainLayout=Q_NULLPTR;
    }
}

void VirtualMoveWidget::SetHeadWidth(int value)
{
    if(headWidget!=Q_NULLPTR){
        headWidget->setFixedHeight(value);
    }
}

使用时,直接继承VirtualMoveWidget类即可

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include "VirtualMoveWidget.h"

class Widget : public VirtualMoveWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
};
#endif // WIDGET_H
#include "widget.h"

Widget::Widget(QWidget *parent)
    : VirtualMoveWidget(parent)
{
    setWindowFlags(Qt::FramelessWindowHint | Qt::Window);
}

Widget::~Widget()
{
}
上一篇:剑指offer——day-1


下一篇:QT-UDP通信