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

Qt自定义标题栏

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

自定义Qt标题栏

根据自己的需要自定义标题栏。

代码

先在MainWindow.cpp中设置this->setWindowFlags(Qt::FramelessWindowHint);,然后:
TitleBar.h

class TitleBar : public QFrame
{
    Q_OBJECT

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

protected:
    //双击标题栏进行界面的最大化/还原
    virtual void mouseDoubleClickEvent(QMouseEvent *event);
    //进行界面的拖动
    virtual void mousePressEvent(QMouseEvent *event);
    virtual void mouseMoveEvent(QMouseEvent *event);
    virtual void mouseReleaseEvent(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; //关闭按钮

    bool m_isPressed;
    QPoint m_startMovePos;
};

TitleBar.cpp

TitleBar::TitleBar(QWidget *parent)
    : QFrame (parent)
    , m_isPressed(false)
{
    //给成员变量申请内存
    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(30, 30);
    m_pIconLabel->setScaledContents(true);
    QPixmap myPix_0_1(":/image/images/icon/icon.png");
    m_pIconLabel->setPixmap(myPix_0_1);

    m_pTitleLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    m_pTitleLabel->setText("REALVIEW");
    m_pTitleLabel->setProperty("title",true);

    //设置按钮的固定大小、图片、取消边框
    m_pMinimizeButton->setIconSize(QSize(31,30));
    m_pMinimizeButton->setIcon(QIcon(":/image/images/icon/min_icon.png"));
    m_pMinimizeButton->setFlat(true);

    //--
    m_pMaximizeButton->setIconSize(QSize(31,30));
    m_pMaximizeButton->setIcon(QIcon(":/image/images/icon/max.png"));
    m_pMaximizeButton->setFlat(true);

    //--
    m_pCloseButton->setIconSize(QSize(31,30));
    m_pCloseButton->setIcon(QIcon(":/image/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->setAlignment(Qt::AlignCenter);
    pLayout->setMargin(0);
//    pLayout->addSpacing(5);
//    pLayout->addWidget(m_pIconLabel);
    pLayout->addSpacing(48);
    pLayout->addWidget(m_pTitleLabel);
    pLayout->addStretch();
    pLayout->addWidget(m_pMinimizeButton);
    pLayout->addSpacing(5);
    pLayout->addWidget(m_pMaximizeButton);
    pLayout->addSpacing(5);
    pLayout->addWidget(m_pCloseButton);
    pLayout->addSpacing(46);
    pLayout->setSpacing(0);
    pLayout->setContentsMargins(0, 0, 0, 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()));

    this->setFixedHeight(44);
    SetWhiteTheme();
//    SetBlackTheme();
}

TitleBar::~TitleBar()
{

}

//双击标题栏进行界面的最大化/还原
void TitleBar::mouseDoubleClickEvent(QMouseEvent *event)
{
    Q_UNUSED(event); //没有实质性的作用,只是用来允许event可以不使用,用来避免编译器警告

    emit m_pMaximizeButton->clicked();
}

//进行界面的拖动  [一般情况下,是界面随着标题栏的移动而移动,所以我们将事件写在标题栏中比较合理]
void TitleBar::mousePressEvent(QMouseEvent *event)
{
    m_isPressed = true;
    m_startMovePos = event->globalPos();
    return QFrame::mousePressEvent(event);
}

void TitleBar::mouseMoveEvent(QMouseEvent *event)
{
    if (m_isPressed)
    {
        QPoint movePoint = event->globalPos() - m_startMovePos;

        if(parentWidget()->parentWidget())
        {
            QPoint widgetPos = this->parentWidget()->parentWidget()->pos();
            m_startMovePos = event->globalPos();
            parentWidget()->parentWidget()->move(widgetPos.x() + movePoint.x(), widgetPos.y() + movePoint.y());
        }
    }
}

void TitleBar::mouseReleaseEvent(QMouseEvent *event)
{
    m_isPressed = false;

    return QFrame::mouseReleaseEvent(event);
}

//使用事件过滤器监听标题栏所在的窗体,所以当窗体标题、图标等信息发生改变时,标题栏也应该随之改变
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();  //窗口最大化/还原显示
            pWindow->isFullScreen() ? pWindow->showNormal() : pWindow->showFullScreen();
        }
        else if (pButton == m_pCloseButton)
        {
            pWindow->close(); //窗口关闭
        }
    }
}

//最大化/还原
void TitleBar::updateMaximize()
{
    QWidget *pWindow = this->window(); //获得标题栏所在的窗口

    if (pWindow->isTopLevel())
    {
        bool bMaximize = pWindow->isFullScreen(); //判断窗口是不是最大化状态,是则返回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());
    }
}

参考

Qt—去掉标题栏后,最大化应用程序窗口时,窗口遮住了任务栏的问题
QT自定义标题栏的可拖拽窗口
Qt之界面(自定义标题栏、无边框、可移动、缩放)