欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Qt 自定义标题栏

程序员文章站 2022-07-13 23:14:32
...

实现

mititlebar.h

/**
* @brief         通用的标题栏控件
* @author        swang
* @date          2020-08-04
*/
#ifndef MITITLEBAR_H
#define MITITLEBAR_H

#include <QWidget>
class QPushButton;
class QLabel;
class MiTiTleBar : public QWidget
{
    Q_OBJECT
public:
    explicit MiTiTleBar(QWidget *parent = nullptr);

    void setTitleIcon   (const QString& imagePath)const;                      /*设置标题图片*/
    void setTitleText   (const QString& titleText)const;                      /*设置标题文字*/
    void setMaxBtnIcon  (const QString& imagePath)const;                      /*设置最大化按钮的图片*/
    void setMinBtnIcon  (const QString& imagePath)const;                      /*设置最小化按钮的图片*/
    void setCloseBtnIcon(const QString& imagePath)const;                      /*设置关闭按钮的图片*/
    void setTitleBg     (const QColor& color){m_color = color;update();}

protected:
    virtual void mouseDoubleClickEvent(QMouseEvent *event)override;           /*双击标题栏进行界面的最大化*/
    virtual bool eventFilter(QObject *obj, QEvent *event)override;            /*进行鼠界面的拖动*/

private slots:
    void on_btnClicked();

private:
    void initForm();
    void updateMaximize();/*最大化/还原*/

    QLabel     * m_pIconLbl;
    QLabel     * m_pTitleLbl;
    QPushButton* m_pMinBtn;
    QPushButton*  m_pMaxBtn;
    QPushButton* m_pCloseBtn;

    QColor       m_color;
};

#endif // MITITLEBAR_H

mititlebar.cpp

/**
* @brief         通用的标题栏
* @author        swang
* @date          2020-08-04
*/

#include <QPushButton>
#include <QLabel>
#include <QPainter>
#include <QHBoxLayout>
#include <QDebug>
#include <QMouseEvent>

#include "mititlebar.h"

MiTiTleBar::MiTiTleBar(QWidget *parent)
    : QWidget(parent)
{
    m_color = QColor(50,50,50,153);

    installEventFilter(this);
    initForm();
}

/**
* @author        swang
* @date          2020-08-04
* @brief         初始化标题栏样式
*/
void MiTiTleBar::initForm()
{
    const QSize btnSize(32,32);
    const QSize iconSize(24,24);

    m_pIconLbl  = new QLabel;
    m_pTitleLbl = new QLabel;
    m_pMinBtn   = new QPushButton;
    m_pMaxBtn   = new QPushButton;
    m_pCloseBtn = new QPushButton;
    this->setObjectName("MiTitle");
    m_pCloseBtn->setObjectName("titleClose");
    m_pIconLbl->setFixedSize(btnSize);
    m_pIconLbl->setScaledContents(true);

    m_pTitleLbl->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);

    m_pMinBtn->setFixedSize(btnSize);
    m_pMaxBtn->setFixedSize(btnSize);
    m_pCloseBtn->setFixedSize(btnSize);

    m_pCloseBtn->setIconSize(iconSize);
    m_pMaxBtn->setIconSize(iconSize);
    m_pMinBtn->setIconSize(iconSize);

    QHBoxLayout *pLayout = new QHBoxLayout(this);
    pLayout->addWidget(m_pIconLbl);
    pLayout->addSpacing(0);
    pLayout->addWidget(m_pTitleLbl);
    pLayout->addWidget(m_pMinBtn);
    pLayout->addWidget(m_pMaxBtn);
    pLayout->addWidget(m_pCloseBtn);
    pLayout->setSpacing(0);
    pLayout->setContentsMargins(0, 0, 0, 0);//左上右下
    setLayout(pLayout);
    connect(m_pMinBtn,&QPushButton::clicked,this,&MiTiTleBar::on_btnClicked);
    connect(m_pMaxBtn,&QPushButton::clicked,this,&MiTiTleBar::on_btnClicked);
    connect(m_pCloseBtn,&QPushButton::clicked,this,&MiTiTleBar::on_btnClicked);
    this->setStyleSheet("QWidget{background-color:rgba(50,50,50,153);}QPushButton{border:none;}"
                        "QPushButton::hover{background-color:rgba(26,26,26,153);}"
                        "QPushButton#titleClose::hover{background-color:rgba(220,20,60,200);}");
}


/**
* @author        swang
* @date          2020-08-04
* @brief         最大化/还原
*/
void MiTiTleBar::updateMaximize()
{
    QWidget *pWindow = this->window();
    if (pWindow->isTopLevel())
    {
        bool bMaximize = pWindow->isMaximized();
        if (bMaximize)
        {
            m_pMaxBtn->setToolTip(tr("还原"));
        }
        else
        {
            m_pMaxBtn->setToolTip(tr("最大化"));
        }
    }
}

/**
* @author        swang
* @date          2020-08-04
* @brief         双击标题栏进行界面的最大化/还原
* @parameter     [event]:鼠标双击事件
*/
void MiTiTleBar::mouseDoubleClickEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
    emit m_pMaxBtn->clicked();
}

/**
* @author        swang
* @date          2020-08-04
* @brief         进行鼠界面的拖动
* @parameter     [obj]:接受事件的对象 | [event]待处理的事件
*/
bool MiTiTleBar::eventFilter(QObject *obj, QEvent *evt)
{

    QWidget *pWindow = this->window();
    static QPoint mousePoint;
    static bool mousePressed = false;
    QMouseEvent *event = static_cast<QMouseEvent *>(evt);
    if(obj->objectName()=="MiTitle"){
        if (event->type() == QEvent::MouseButtonPress) {
            if (event->button() == Qt::LeftButton) {
                mousePressed = true;
                mousePoint = event->globalPos() - pWindow->pos();
                return true;
            }
        } else if (event->type() == QEvent::MouseButtonRelease) {
            mousePressed = false;
            return true;
        } else if (event->type() == QEvent::MouseMove) {
            if (mousePressed && (event->buttons() & Qt::LeftButton)) {
                pWindow->move(event->globalPos() - mousePoint);
                return true;
            }
        }
    }
    return QWidget::eventFilter(obj,evt);

}

/**
        * @author        swang
        * @date          2020-08-04
        * @brief         响应点击事件
        */
void MiTiTleBar::on_btnClicked()
{
    QPushButton *pButton = qobject_cast<QPushButton *>(sender());
    QWidget *pWindow = this->window();
    if (pWindow->isTopLevel())
    {
        if (pButton == m_pMinBtn)
        {
            pWindow->showMinimized();
        }
        else if (pButton == m_pMaxBtn)
        {
            pWindow->isMaximized() ? pWindow->showNormal() : pWindow->showMaximized();
        }
        else if (pButton == m_pCloseBtn)
        {
            pWindow->close();
        }
    }

}


void MiTiTleBar::setTitleIcon(const QString &imagePath)const
{
    QPixmap image(imagePath);      //保持长宽比          //平滑转换
    QPixmap fitpixmap=image.scaled(m_pIconLbl->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
    m_pIconLbl->setPixmap(fitpixmap);
}

void MiTiTleBar::setTitleText(const QString &titleText) const
{
    m_pTitleLbl->setText(titleText);
}

void MiTiTleBar::setMaxBtnIcon(const QString &imagePath) const
{
    QPixmap image(imagePath);      //保持长宽比          //平滑转换
    QPixmap fitpixmap=image.scaled(m_pMaxBtn->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
    m_pMaxBtn->setIcon(QIcon(fitpixmap));
}

void MiTiTleBar::setMinBtnIcon(const QString &imagePath) const
{
    QPixmap image(imagePath);
    QPixmap fitpixmap=image.scaled(m_pMinBtn->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
    m_pMinBtn->setIcon(QIcon(fitpixmap));
}

void MiTiTleBar::setCloseBtnIcon(const QString &imagePath) const
{
    QPixmap image(imagePath);
    QPixmap fitpixmap=image.scaled(m_pCloseBtn->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
    m_pCloseBtn->setIcon(QIcon(fitpixmap));
}









 

调用:

void Widget:: initForm()
{
    setWindowFlags(Qt::FramelessWindowHint);
    QVBoxLayout * pVbox = new QVBoxLayout;
    MiTiTleBar* pTitleBar = new MiTiTleBar;
    pVbox->setContentsMargins(0,0,0,0);
    pVbox->addWidget(pTitleBar);
    pVbox->addStretch();
    this->setLayout(pVbox);
    this->installEventFilter(pTitleBar);
}

 

上一篇: qt自定义标题栏

下一篇: WPF学习