Qt之界面(自定義標題欄、無邊框、可移動、縮放)


效果


自定義標題欄

titleBar.h

#ifndef TITLEBAR_H
#define TITLEBAR_H

#include <QLabel>
#include <QPushButton>

class titleBar : public QWidget
{
    Q_OBJECT

public:
    explicit titleBar(QWidget *parent = nullptr);
    ~titleBar();

protected:

    //雙擊標題欄進行界面的最大化/還原
    virtual void mouseDoubleClickEvent(QMouseEvent *event);

    //進行界面的拖動
    virtual void mousePressEvent(QMouseEvent *event);

    //設置界面標題與圖標
    virtual bool eventFilter(QObject *obj, QEvent *event);

private slots:

    //進行最小化、最大化/還原、關閉操作
    void onClicked();

private:

    //最大化/還原
    void updateMaximize();

private:
    QLabel *m_pIconLabel; //標題欄圖標
    QLabel *m_pTitleLabel; //標題欄標題
    QPushButton *m_pMinimizeButton; //最小化按鈕
    QPushButton *m_pMaximizeButton; //最大化/還原按鈕
    QPushButton *m_pCloseButton; //關閉按鈕
};

#endif // TITLEBAR_H

titleBar.cpp

#include <QLabel>
#include <QPushButton>
#include <QHBoxLayout>
#include <QEvent>
#include <QMouseEvent>
#include <QApplication>
#include "titleBar.h"

//調用WIN API需要用到的頭文件與庫
#ifdef Q_OS_WIN
#pragma comment(lib, "user32.lib")
#include <qt_windows.h>
#endif

titleBar::titleBar(QWidget *parent)
    : QWidget(parent)
{
    setFixedHeight(30);

    //給成員變量申請內存
    m_pIconLabel = new QLabel(this);
    m_pTitleLabel = new QLabel(this);
    m_pMinimizeButton = new QPushButton(this);
    m_pMaximizeButton = new QPushButton(this);
    m_pCloseButton = new QPushButton(this);

    //初始化圖標Label
    m_pIconLabel->setFixedSize(20, 20);
    m_pIconLabel->setScaledContents(true);

    m_pTitleLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    //設置按鈕的固定大小、圖片、取消邊框
    m_pMinimizeButton->setIconSize(QSize(27,22));
    m_pMinimizeButton->setIcon(QIcon("://Images/icon_min.png"));
    m_pMinimizeButton->setFlat(true);
    //--
    m_pMaximizeButton->setIconSize(QSize(27,22));
    m_pMaximizeButton->setIcon(QIcon("://Images/icon_max.png"));
    m_pMaximizeButton->setFlat(true);
    //--
    m_pCloseButton->setIconSize(QSize(27,22));
    m_pCloseButton->setIcon(QIcon("://Images/icon_close.png"));
    m_pCloseButton->setFlat(true);

    //設置窗口部件的名稱
    m_pTitleLabel->setObjectName("whiteLabel");
    m_pMinimizeButton->setObjectName("minimizeButton");
    m_pMaximizeButton->setObjectName("maximizeButton");
    m_pCloseButton->setObjectName("closeButton");


    //給按鈕設置靜態tooltip,當鼠標移上去時顯示tooltip
    m_pMinimizeButton->setToolTip("Minimize");
    m_pMaximizeButton->setToolTip("Maximize");
    m_pCloseButton->setToolTip("Close");

    //標題欄布局
    QHBoxLayout *pLayout = new QHBoxLayout(this);
    pLayout->addWidget(m_pIconLabel);
    pLayout->addSpacing(5);
    pLayout->addWidget(m_pTitleLabel);
    pLayout->addWidget(m_pMinimizeButton);
    pLayout->addWidget(m_pMaximizeButton);
    pLayout->addWidget(m_pCloseButton);
    pLayout->setSpacing(0);
    pLayout->setContentsMargins(5, 0, 5, 0);
    setLayout(pLayout);

    //連接三個按鈕的信號槽
    connect(m_pMinimizeButton, SIGNAL(clicked(bool)), this, SLOT(onClicked()));
    connect(m_pMaximizeButton, SIGNAL(clicked(bool)), this, SLOT(onClicked()));
    connect(m_pCloseButton, SIGNAL(clicked(bool)), this, SLOT(onClicked()));
}

titleBar::~titleBar()
{

}

//雙擊標題欄進行界面的最大化/還原
void titleBar::mouseDoubleClickEvent(QMouseEvent *event)
{
    Q_UNUSED(event); //沒有實質性的作用,只是用來允許event可以不使用,用來避免編譯器警告

    emit m_pMaximizeButton->clicked();
}

//進行界面的拖動  [一般情況下,是界面隨着標題欄的移動而移動,所以我們將事件寫在標題欄中比較合理]
void titleBar::mousePressEvent(QMouseEvent *event)
{
#ifdef Q_OS_WIN
    if (ReleaseCapture())
    {
        QWidget *pWindow = this->window();
        if (pWindow->isTopLevel())
        {
           SendMessage(HWND(pWindow->winId()), WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }
    }
       event->ignore();
#else
#endif
}

//使用事件過濾器監聽標題欄所在的窗體,所以當窗體標題、圖標等信息發生改變時,標題欄也應該隨之改變
bool titleBar::eventFilter(QObject *obj, QEvent *event)
{
    switch ( event->type() ) //判斷發生事件的類型
    {
        case QEvent::WindowTitleChange: //窗口標題改變事件
        {
            QWidget *pWidget = qobject_cast<QWidget *>(obj); //獲得發生事件的窗口對象
            if (pWidget)
            {
                //窗體標題改變,則標題欄標題也隨之改變
                m_pTitleLabel->setText(pWidget->windowTitle());
                return true;
            }
        }
        break;

        case QEvent::WindowIconChange: //窗口圖標改變事件
        {
            QWidget *pWidget = qobject_cast<QWidget *>(obj);
            if (pWidget)
            {
                //窗體圖標改變,則標題欄圖標也隨之改變
                QIcon icon = pWidget->windowIcon();
                m_pIconLabel->setPixmap(icon.pixmap(m_pIconLabel->size()));
                return true;
            }
        }
        break;

        case QEvent::Resize:
            updateMaximize(); //最大化/還原
            return true;

        default:
        return QWidget::eventFilter(obj, event);
    }

    return QWidget::eventFilter(obj, event);
}

//進行最小化、最大化/還原、關閉操作
void titleBar::onClicked()
{
    //QObject::Sender()返回發送信號的對象的指針,返回類型為QObject *
    QPushButton *pButton = qobject_cast<QPushButton *>(sender());

    QWidget *pWindow = this->window(); //獲得標題欄所在的窗口

    if (pWindow->isTopLevel())
    {
        //判斷發送信號的對象使哪個按鈕
        if (pButton == m_pMinimizeButton)
        {
            pWindow->showMinimized(); //窗口最小化顯示
        }
        else if (pButton == m_pMaximizeButton)
        {
            pWindow->isMaximized() ? pWindow->showNormal() : pWindow->showMaximized();  //窗口最大化/還原顯示
        }
        else if (pButton == m_pCloseButton)
        {
            pWindow->close(); //窗口關閉
        }
    }
}

//最大化/還原
void titleBar::updateMaximize()
{
    QWidget *pWindow = this->window(); //獲得標題欄所在的窗口

    if (pWindow->isTopLevel())
    {
        bool bMaximize = pWindow->isMaximized(); //判斷窗口是不是最大化狀態,是則返回true,否則返回false
        if (bMaximize)
        {
            //目前窗口是最大化狀態,則最大化/還原的toolTip設置為"Restore"
            m_pMaximizeButton->setToolTip(tr("Restore"));
            //設置按鈕的屬性名為"maximizeProperty"
            m_pMaximizeButton->setProperty("maximizeProperty", "restore");
        }
        else
        {
            //目前窗口是還原狀態,則最大化/還原的toolTip設置為"Maximize"
            m_pMaximizeButton->setToolTip(tr("Maximize"));
            //設置按鈕的屬性名為"maximizeProperty"
            m_pMaximizeButton->setProperty("maximizeProperty", "maximize");
        }

        m_pMaximizeButton->setStyle(QApplication::style());
    }
}

界面

widget.h

#ifndef WIDGET_H
#define WIDGET_H

/*************************************************************************************
 * 功能:實現自定義窗體的無邊框與移動,以及自定義標題欄-用來顯示窗體的圖標、標題,以及控制窗體最小化、最大化/還原、關閉、縮放(僅支持windows平台)。
*************************************************************************************/

#include <QWidget>
#include <QMouseEvent>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();

bool nativeEvent(const QByteArray &eventType, void *message, long *result);

private:
    Ui::Widget *ui;

    int m_nBorderWidth; //m_nBorder表示鼠標位於邊框縮放范圍的寬度
};

#endif // WIDGET_H

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include "titlebar.h" //包含“自定義標題欄”頭文件
#include <QVBoxLayout>

//調用WIN API需要用到的頭文件與庫 [實現縮放]
#ifdef Q_OS_WIN
#include <qt_windows.h>
#include <Windowsx.h>
#endif

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    //Qt::FramelessWindowHint設置窗口標志為無邊框,而Qt::WindowStaysOnTopHint使窗口位於所有界面之上
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    //背景透明
    //setAttribute(Qt::WA_TranslucentBackground, true);

    //定義自定義標題欄對象
    titleBar *pTitleBar = new titleBar(this);
    installEventFilter(pTitleBar);

    resize(400, 300);
    setWindowTitle("Custom Window"); //設置窗口名稱,會發生窗口標題欄改變事件,隨之自定義標題欄的標題會更新
    setWindowIcon(QIcon("://Images/icon.png")); //設置窗口圖標,會發生窗口圖標改變事件,隨之自定義標題欄的圖標會更新

    //使用調色板設置窗口的背景色
    QPalette pal(palette());
    pal.setColor(QPalette::Background, QColor(150, 150, 150));
    setAutoFillBackground(true);
    setPalette(pal);

    //窗口布局中加入標題欄
    QVBoxLayout *pLayout = new QVBoxLayout();
    pLayout->addWidget(pTitleBar);
    pLayout->addStretch();
    pLayout->setSpacing(0);
    pLayout->setContentsMargins(0, 0, 0, 0);
    setLayout(pLayout);

    //m_nBorder表示鼠標位於邊框縮放范圍的寬度,可以設置為5
    m_nBorderWidth=5;
}

Widget::~Widget()
{
    delete ui;
}

//nativeEvent主要用於進程間通信-消息傳遞,使用這種方式后來實現窗體的縮放 [加上了這函數,窗口也能移動了]
bool Widget::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
    Q_UNUSED(eventType)

    MSG *param = static_cast<MSG *>(message);

    switch (param->message)
    {
    case WM_NCHITTEST:
    {
        int nX = GET_X_LPARAM(param->lParam) - this->geometry().x();
        int nY = GET_Y_LPARAM(param->lParam) - this->geometry().y();

        // 如果鼠標位於子控件上,則不進行處理
        if (childAt(nX, nY) != nullptr)
            return QWidget::nativeEvent(eventType, message, result);

        *result = HTCAPTION;

        // 鼠標區域位於窗體邊框,進行縮放
        if ((nX > 0) && (nX < m_nBorderWidth))
            *result = HTLEFT;

        if ((nX > this->width() - m_nBorderWidth) && (nX < this->width()))
            *result = HTRIGHT;

        if ((nY > 0) && (nY < m_nBorderWidth))
            *result = HTTOP;

        if ((nY > this->height() - m_nBorderWidth) && (nY < this->height()))
            *result = HTBOTTOM;

        if ((nX > 0) && (nX < m_nBorderWidth) && (nY > 0)
                && (nY < m_nBorderWidth))
            *result = HTTOPLEFT;

        if ((nX > this->width() - m_nBorderWidth) && (nX < this->width())
                && (nY > 0) && (nY < m_nBorderWidth))
            *result = HTTOPRIGHT;

        if ((nX > 0) && (nX < m_nBorderWidth)
                && (nY > this->height() - m_nBorderWidth) && (nY < this->height()))
            *result = HTBOTTOMLEFT;

        if ((nX > this->width() - m_nBorderWidth) && (nX < this->width())
                && (nY > this->height() - m_nBorderWidth) && (nY < this->height()))
            *result = HTBOTTOMRIGHT;

        return true;
    }
    }

    return QWidget::nativeEvent(eventType, message, result);
}

參考

一去丶二三里


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM