#include "titleBar.h" #include #include #include TitleBar::TitleBar(QWidget *parent,bool bClose,bool bMaxMin) : QWidget(parent) , m_parentWindow(parent) ,_bShowClose(bClose) ,_bShowMaxMin(bMaxMin) { setupUI(); setFixedHeight(25); // 稍微矮一点 setAttribute(Qt::WA_StyledBackground, true); // 启用样式背景 setAttribute(Qt::WA_NoSystemBackground, false); // 确保有自己的调色板 setAutoFillBackground(true); // 使用更具体的样式表 QString style = R"( QWidget { background: #2c5282; } QLabel { color: white; font-size: 12px; background: transparent; } QPushButton { background: transparent; border: none; color: white; min-width: 20px; min-height: 20px; } )"; setStyleSheet(style); if (m_parentWindow) { m_parentWindow->installEventFilter(this); } } void TitleBar::setupUI() { QHBoxLayout *layout = new QHBoxLayout(this); layout->setContentsMargins(4, 0, 0, 0); layout->setSpacing(0); // 标题 m_titleLabel = new QLabel("窗口标题"); m_titleLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); layout->addWidget(m_titleLabel); /* ===== 中间占位,用于居中 ===== */ layout->addStretch(); /* ===== 中间内容 Label ===== */ m_centerLabel = new QLabel(); m_centerLabel->setAlignment(Qt::AlignCenter); m_centerLabel->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred); layout->addWidget(m_centerLabel); /* ===== 右侧占位 ===== */ layout->addStretch(); if(_bShowMaxMin){ // 最大化/还原按钮 m_maximizeButton = new QPushButton(tr("🗖")); // 最大化图标 m_maximizeButton->setObjectName("maximizeButton"); m_maximizeButton->setToolTip("最大化"); layout->addWidget(m_maximizeButton); connect(m_maximizeButton, &QPushButton::clicked, this, &TitleBar::maximizeClicked); } if(_bShowClose){ // 关闭按钮 m_closeButton = new QPushButton("×"); m_closeButton->setObjectName("closeButton"); m_closeButton->setToolTip("关闭"); layout->addWidget(m_closeButton); connect(m_closeButton, &QPushButton::clicked, this, &TitleBar::closeClicked); } } void TitleBar::setTitle(const QString &title) { m_titleLabel->setText(title); } void TitleBar::setCenterText(const QString &text) { if (m_centerLabel) m_centerLabel->setText(text); } void TitleBar::updateMaximizeButton() { if(_bShowMaxMin){ if (m_parentWindow) { m_isMaximized = m_parentWindow->isMaximized(); if (m_isMaximized) { m_maximizeButton->setText(tr("🗗")); // 还原图标 m_maximizeButton->setToolTip("还原"); } else { m_maximizeButton->setText(tr("🗖")); // 最大化图标 m_maximizeButton->setToolTip("最大化"); } } } } void TitleBar::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { m_dragStartPosition = event->globalPosition().toPoint() - m_parentWindow->frameGeometry().topLeft(); event->accept(); } } void TitleBar::mouseMoveEvent(QMouseEvent *event) { if (event->buttons() & Qt::LeftButton) { m_parentWindow->move(event->globalPosition().toPoint() - m_dragStartPosition); event->accept(); } } void TitleBar::mouseDoubleClickEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { emit maximizeClicked(); } } bool TitleBar::eventFilter(QObject *watched, QEvent *event) { if (watched == m_parentWindow && event->type() == QEvent::Show) { // 防止最大化/全屏时干扰 if (m_parentWindow->isMaximized() || m_parentWindow->isFullScreen()) { return false; } QScreen *screen = QGuiApplication::primaryScreen(); if (!screen) return false; QRect screenRect = screen->availableGeometry(); QRect windowRect = m_parentWindow->geometry(); // ✅ 正确 int x = screenRect.x() + (screenRect.width() - windowRect.width()) / 2; int y = screenRect.y() + (screenRect.height() - windowRect.height()) / 2; m_parentWindow->move(x, y); } return QWidget::eventFilter(watched, event); }