• 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);
    }
    

    参考

    一去丶二三里

  • 相关阅读:
    远程获取图片尺寸
    python httplib get和post获取数据
    python 之 logging
    php artisan常用方法
    html页面制作css基本设置
    zlib-1.2.7/libpng-1.5.9 instead of zlib-1.2.8/libpng-1.6.6
    shell脚本自动拉起启动程序
    php截取中文无乱码
    路飞学城1之课程与课程详细
    vuex(数据商店实现思想)day06
  • 原文地址:https://www.cnblogs.com/linuxAndMcu/p/10609182.html
Copyright © 2020-2023  润新知