【Qt】自定义无边框界面

无边框窗体效果图
给大家分享下源码,喜欢就点个赞拿去用吧:

// qframelesswidget.h
#ifndef QFRAMELESSWIDGET_H
#define QFRAMELESSWIDGET_H

#include <QWidget>
#include <QMouseEvent>
#include <QPaintEvent>
#include <QString>

class QPushButton;
class QLabel;
class QFramelessWidget : public QWidget
{
    Q_OBJECT
public:
    QFramelessWidget(QString icon = "",QString title = QString("qFramelseewidget"),QWidget *parent = 0);
    virtual ~QFramelessWidget();
private slots:
    void onMin( bool );
    void onMax(bool);
    void onClose( bool );
private:
    bool minOrNormal;//true表示当前窗口状态为normal,图标应显示为max
public:
    QPushButton* m_minOrNormalPushBtn; // 显示最大或者最小的图标
    QPushButton* m_maxPushBtn; // 最大化按钮
    QPushButton* m_closePushBtn;// 关闭按钮
    QLabel* m_label; // 图标
    QString m_icon; // m_label中图标路径
    QLabel* m_titlelabel; // 标题
    QString m_title;
    QWidget* titleWidget;
    QWidget* centerwidget;
    QImage image; // 背景图片
public:
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void resizeEvent(QResizeEvent* event);
    /// 窗体移动事件的点
    QPoint windowPos;
    QPoint mousePos;
    QPoint dPos;
public:
    void setBackgroundImage(QString filename); // 设置背景图片
    bool hasBackgroundImage;
    void setIcon(QString icon); // 设置图标
    void setTittle(QString tittle); // 设置标题
protected:
    void initWindowTitle();
    void setThisLayout();
    void setThisStyle();
};

#endif // QFRAMELESSWIDGET_H

// qframelesswidget.cpp
#include "qframelesswidget.h"
#include <QPushButton>
#include <QLabel>
#include <QIcon>
#include <QStyle>
#include <QVBoxLayout>

#define BTNSIZE 30

QFramelessWidget::QFramelessWidget(QString icon, QString title, QWidget *parent)
    :m_icon(icon),m_title(title),QWidget(parent)
{
    this->setWindowFlags(Qt::FramelessWindowHint |Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
    initWindowTitle (); // 初始化窗体
    setIcon (m_icon);// 设置图标
    setTittle (m_title);// 设置标题
    hasBackgroundImage = false; // 默认无背景图片
}

QFramelessWidget::~QFramelessWidget(){}

void QFramelessWidget::setIcon(QString icon){
    QPixmap pixmap(icon);
    m_label->setPixmap (pixmap);
}
void QFramelessWidget::setTittle(QString tittle){
     m_titlelabel->setText (tittle);
}

void QFramelessWidget::setBackgroundImage(QString filename)
{
    hasBackgroundImage = true;

    image.load (filename);
    setAutoFillBackground (true);
    QPalette pal(palette());
    pal.setBrush(QPalette::Window,
                 QBrush(image.scaled(size(), Qt::IgnoreAspectRatio,
                                     Qt::SmoothTransformation)));
    setPalette(pal);
}

void QFramelessWidget::initWindowTitle()
{
    this->resize (500,300); /// 默认设置大小为500*300
    minOrNormal = true; /// 默认显示普通大小窗体
    titleWidget = new QWidget(this);
    m_minOrNormalPushBtn = new QPushButton(titleWidget);
    m_maxPushBtn = new QPushButton(titleWidget);
    m_closePushBtn = new QPushButton(titleWidget);
    m_label = new QLabel(titleWidget);
    m_titlelabel = new QLabel(titleWidget);
    centerwidget = new QWidget(this); /// 主窗体,所有继承于QFramelessWidget的子类,其父对象为centerwidget
    /// 设置布局器
    QVBoxLayout* vlayout = new QVBoxLayout(this);
    vlayout->addWidget (titleWidget);
    vlayout->addWidget (centerwidget);
    this->setLayout (vlayout);
    /// 设置控件位置和风格样式
    setThisLayout ();
    setThisStyle ();

    connect (m_closePushBtn,SIGNAL(clicked(bool)),SLOT(onClose(bool)));
    connect (m_maxPushBtn,SIGNAL(clicked(bool)),SLOT(onMax(bool)));
    connect (m_minOrNormalPushBtn,SIGNAL(clicked(bool)),SLOT(onMin(bool)));
}

void QFramelessWidget::setThisStyle(){
    /// 默认为扁平风格
    m_closePushBtn->setFlat(true);
    m_minOrNormalPushBtn->setFlat(true);
    m_maxPushBtn->setFlat (true);

    m_closePushBtn->setStyleSheet("QPushButton{border-style: none/*;border-top-right-radius:5px*/}"
                                  "QPushButton:hover{background-color:red;color: white;}"
                                  "QPushButton:pressed{background-color:rgba(85, 170, 255,200); border-style: inset; }");
    m_minOrNormalPushBtn->setStyleSheet("QPushButton{border-style: none;}"
                                        "QPushButton:hover{background-color:lightgray; color: white;}"
                                        "QPushButton:pressed{background-color:rgb(85, 170, 255); border-style: inset; }");
    m_maxPushBtn->setStyleSheet("QPushButton{border-style: none;}"
                                "QPushButton:hover{background-color:lightgray; color: white;}"
                                "QPushButton:pressed{background-color:rgb(85, 170, 255); border-style: inset; }");


    m_closePushBtn->setCursor(Qt::PointingHandCursor);
    m_maxPushBtn->setCursor(Qt::PointingHandCursor);
    m_minOrNormalPushBtn->setCursor(Qt::PointingHandCursor);
    QIcon icon;
    if( minOrNormal ){/// true means normal
        /// normal时显示最小化图标
        icon = style()->standardIcon( QStyle::SP_TitleBarMinButton );
        m_minOrNormalPushBtn->setIcon( icon );
        m_minOrNormalPushBtn->setToolTip(QObject::tr("Min"));

        icon = style()->standardIcon (QStyle::SP_TitleBarMaxButton);
        m_maxPushBtn->setIcon( icon );
        m_maxPushBtn->setToolTip(QObject::tr("Max"));
    }
    icon = style()->standardIcon( QStyle::SP_TitleBarCloseButton );
    m_closePushBtn->setIcon( icon );
    m_closePushBtn->setToolTip(QObject::tr("Quit"));
}

void QFramelessWidget::setThisLayout(){
    centerwidget->setGeometry (0,BTNSIZE,width (),height ()-BTNSIZE);
    titleWidget->setGeometry (0,0,width (),BTNSIZE);
    m_label->setGeometry (0,0,BTNSIZE,BTNSIZE);
    m_titlelabel->setGeometry (BTNSIZE+5,0,titleWidget->width ()-4*BTNSIZE -5,BTNSIZE);
    m_closePushBtn->setGeometry (titleWidget->width () - BTNSIZE,0,BTNSIZE,BTNSIZE);
    m_maxPushBtn->setGeometry (titleWidget->width () - 2*BTNSIZE,0,BTNSIZE,BTNSIZE);
    m_minOrNormalPushBtn->setGeometry (titleWidget->width () - 3*BTNSIZE,0,BTNSIZE,BTNSIZE);
}


void QFramelessWidget::mousePressEvent(QMouseEvent *event)
{
    this->windowPos = this->pos();           // 获得部件当前位置
    this->mousePos = event->globalPos();     // 获得鼠标位置
    this->dPos = mousePos - windowPos;       // 移动后部件所在的位置
}

void QFramelessWidget::mouseMoveEvent(QMouseEvent *event)
{
    this->move(event->globalPos() - this->dPos);
}

void QFramelessWidget::resizeEvent(QResizeEvent *event)
{
    Q_UNUSED(event)
    /// 控件位置调整
    setThisLayout ();

    /// 背景图片缩放
    if(hasBackgroundImage){
        QPalette pal(palette());
        pal.setBrush(QPalette::Window,QBrush(image.scaled(event->size(), Qt::IgnoreAspectRatio,Qt::SmoothTransformation)));
        setPalette(pal);
    }
}

void QFramelessWidget::onMin(bool)
{
    if( windowState() != Qt::WindowMinimized ){
        setWindowState( Qt::WindowMinimized );
    }
}

void QFramelessWidget::onMax(bool)
{
    if( windowState() != Qt::WindowMaximized ){
        setWindowState( Qt::WindowMaximized );
    }else{
        setWindowState( Qt::WindowNoState );
    }
}

void QFramelessWidget::onClose(bool)
{
    emit close();
}

// main.cpp
#include "qframelesswidget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QFramelessWidget w(":/app.png");
    w.setTittle (QString("girl.jpg"));
    w.setBackgroundImage (":/girl.jpg");
    w.show();

    return a.exec();
}

测试用的图片就不提供了,自定义设置就好。其他,有机会再做完善。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

键盘会跳舞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值