QMainWindow无边框(自定义标题栏)

该博客介绍了如何在Qt中重写QMainWindow的标题栏,以实现Office2016风格的Ribbon效果或者通过QWidget插入自定义标题栏,并提供了鼠标事件处理来实现在窗口内的拖动功能。博主给出了头文件和源文件的代码示例,展示了设置窗口无边框并添加最小化、最大化和关闭按钮的操作。
摘要由CSDN通过智能技术生成

一.问题概述及思路

问题描述:QMainWindow默认调用windows系统自带边框,部分情况不符合使用需求。
解决思路:
1.重写QMenuBar插入MainWindow
Qt实现Offce2016系列Ribbon效果
2.QWidget插入MainWindow(setMenuWidget(w);本文采用方法)
3.重写QMainWindow。
参考Github上

二.代码

头文件

#ifndef KQTILTLE_H
#define KQTILTLE_H

#include <QWidget>
#include <QPushButton>

namespace Ui {
class KQTiltle;
}

class KQTiltle : public QWidget
{
    Q_OBJECT
public:
    explicit KQTiltle(QWidget *parent = 0);
    ~KQTiltle();
protected:
    virtual void mousePressEvent(QMouseEvent *event);
    virtual void mouseReleaseEvent(QMouseEvent *event);
    virtual void mouseMoveEvent(QMouseEvent *event);

private slots:
    void on_toolButton_minimize_clicked();
    void on_toolButton_window_clicked();
    void on_toolButton_close_clicked();

    void on_pushBtn_clicked();

public:
    QPushButton* m_btCloseCurPage;      //关闭当前页按钮
private:
    Ui::KQTiltle *ui;

    QPoint m_PointOldPos;
    bool m_bMouseDown;
    bool m_CheckIsRunning;  //标记任务检查页当前是否在进行任务检查   当进行检查时,跳转页的槽禁止响应
};

#endif // KQTILTLE_H

源文件:

#include "kqtiltle.h"
#include "ui_kqtiltle.h"
//#include <QMenu>
#include <QMessageBox>
#include <QMouseEvent>
#include "mainwindow.h"
#include "Dlg.h"

KQTiltle::KQTiltle(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::KQTiltle)
  ,m_CheckIsRunning(false)
  ,m_btCloseCurPage(nullptr)
{
    ui->setupUi(this);

    //    setMouseTracking(true);
    //    ui->widget_TitleTool->setMouseTracking(true);
    //    ui->widget_menu->setMouseTracking(true);
    ui->widget_menu->layout()->setAlignment(ui->toolButton_help,Qt::AlignTop);

    m_btCloseCurPage = ui->pushButton_closeCurPage;
    //m_btCloseCurPage->hide();

    ui->pushButton_CheckSet->hide();
}

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

void KQTiltle::on_toolButton_minimize_clicked()
{
    QWidget* pWindow=parentWidget()->parentWidget();
    if(pWindow)
    {
        pWindow->showMinimized();
    }
}

void KQTiltle::on_toolButton_window_clicked()
{
    QWidget* pWindow=parentWidget()->parentWidget();
    if(pWindow)
    {
        pWindow->isMaximized() ? pWindow->showNormal() : pWindow->showMaximized();
    }
}

void KQTiltle::on_toolButton_close_clicked()
{
//    int nReg = QMessageBox::question(this,QString::fromLocal8Bit("提示"),QString::fromLocal8Bit("你确定要关闭吗?"),QMessageBox::Yes | QMessageBox::No);
//    if(nReg ==QMessageBox::Yes)
//    {
        QWidget* pWindow=parentWidget()->parentWidget();
        if(pWindow)
        {
            pWindow->close();
        }
//    }
}

void KQTiltle::mousePressEvent(QMouseEvent *e)
{
    if(e->pos().y()>150) return;

    m_PointOldPos = e->pos();
    m_bMouseDown = e->button() == Qt::LeftButton;
}

void KQTiltle::mouseMoveEvent(QMouseEvent *e)
{
    int x = e->x();
    int y = e->y();

    if (m_bMouseDown) {
        int dx = x - m_PointOldPos.x();
        int dy = y - m_PointOldPos.y();

        QWidget* pWindow=parentWidget()->parentWidget();
        QRect g = pWindow->geometry();
        g.setRect(g.x()+dx,g.y()+dy,g.width(),g.height());
        pWindow->setGeometry(g);
    }
}

void KQTiltle::mouseReleaseEvent(QMouseEvent *e)
{
    m_bMouseDown = false;
}

void KQTiltle::on_pushBtn_clicked()
{
    Dlg* dlg = new Dlg(this);
    dlg->exec();
}

调用

setWindowFlags(Qt::Window|Qt::FramelessWindowHint |Qt::WindowSystemMenuHint|Qt::WindowMinimizeButtonHint|Qt::WindowMaximizeButtonHint);

    QWidget* w=new QWidget(this);
    w->setObjectName("mTitle");
    m_titleBar=new KQTiltle(w);
    w->setLayout(new QHBoxLayout(w));
    w->layout()->setSpacing(0);
    w->layout()->setMargin(0);
    w->layout()->addWidget(m_titleBar);
    this->setMenuWidget(w);
以下是使用PyQt实现自定义标题栏的示例代码: ```python from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QLabel, QPushButton from PyQt5.QtCore import Qt from PyQt5.QtGui import QColor class CustomTitleBar(QWidget): def __init__(self, parent): super().__init__(parent) self.setFixedHeight(30) layout = QVBoxLayout(self) layout.setContentsMargins(0, 0, 0, 0) layout.setSpacing(0) self.title_label = QLabel("Custom Title Bar") self.title_label.setAlignment(Qt.AlignCenter) layout.addWidget(self.title_label) self.minimize_button = QPushButton("-") self.minimize_button.setFixedSize(30, 30) self.minimize_button.clicked.connect(self.parent().showMinimized) layout.addWidget(self.minimize_button) self.maximize_button = QPushButton("□") self.maximize_button.setFixedSize(30, 30) self.maximize_button.clicked.connect(self.toggleMaximize) layout.addWidget(self.maximize_button) self.close_button = QPushButton("×") self.close_button.setFixedSize(30, 30) self.close_button.clicked.connect(self.parent().close) layout.addWidget(self.close_button) layout.addStretch() self.setStyleSheet(""" background-color: #333333; color: white; font-size: 14px; font-weight: bold; padding-left: 5px; padding-right: 5px; """) def toggleMaximize(self): if self.parent().isMaximized(): self.parent().showNormal() self.maximize_button.setText("□") else: self.parent().showMaximized() self.maximize_button.setText("◻") class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Custom Title Bar Example") self.setWindowFlags(Qt.FramelessWindowHint) self.setAttribute(Qt.WA_TranslucentBackground) self.title_bar = CustomTitleBar(self) self.setCentralWidget(QWidget(self)) self.centralWidget().setLayout(QVBoxLayout()) self.centralWidget().layout().addWidget(QLabel("Content Area")) self.setStyleSheet(""" QMainWindow { background-color: white; } """) self.show() app = QApplication([]) window = MainWindow() app.exec_() ``` 这段代码创建了一个自定义标题栏,其中包含了一个标题标签、最小化按钮、最大化/还原按钮和关闭按钮。通过设置窗口的`setWindowFlags(Qt.FramelessWindowHint)`和`setAttribute(Qt.WA_TranslucentBackground)`属性,使窗口无边框并且背景透明。然后,将自定义标题栏添加到主窗口的顶部,并在主窗口的中央添加一个内容区域。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值