赞
踩
窗口分为三个控件:主窗口QMainWindows,标题栏QWidget,客户端QWidget
#pragma once #include <QWidget> class uititlebar : public QWidget { Q_OBJECT public: uititlebar(QWidget *parent = Q_NULLPTR); ~uititlebar(); protected: virtual void mousePressEvent(QMouseEvent* event) override; virtual void mouseMoveEvent(QMouseEvent* event) override; virtual void mouseReleaseEvent(QMouseEvent* event) override; virtual void mouseDoubleClickEvent(QMouseEvent* event) override; private Q_SLOTS: void on_btn_clicked(); private: void initUI(); void updateMaximize(); QPushButton* m_btnMin; QPushButton* m_btnMax; QPushButton* m_btnClose; bool m_isPressed = false; QPoint m_startMovePos; };
#include "stdafx.h" #include "uititlebar.h" uititlebar::uititlebar(QWidget *parent) : QWidget(parent) { this->setAutoFillBackground(true); // 自动刷新背景 QPalette palette(this->palette()); palette.setColor(QPalette::Background, RGB(255, 255, 255)); this->setPalette(palette); initUI(); } uititlebar::~uititlebar() { } void uititlebar::initUI() { setFixedHeight(30); QHBoxLayout* _vlayoutMain = new QHBoxLayout(this); QLabel* _lbeLogo = new QLabel(this); _lbeLogo->setObjectName("logoLabel"); _lbeLogo->setFixedSize(22, 22); _lbeLogo->setAlignment(Qt::AlignTop); // 最小化 最大化 关闭 m_btnMin = new QPushButton(this); m_btnMin->setObjectName("minButton"); m_btnMin->setFixedSize(18, 18); m_btnMin->setToolTip(tr("最小化")); m_btnMax = new QPushButton(this); m_btnMax->setObjectName("maxButton"); m_btnMax->setFixedSize(18, 18); m_btnMax->setToolTip(tr("最大化")); m_btnClose = new QPushButton(this); m_btnClose->setObjectName("closeButton"); m_btnClose->setFixedSize(18, 18); m_btnClose->setToolTip(tr("关闭")); _vlayoutMain->addWidget(_lbeLogo); _vlayoutMain->addStretch(); _vlayoutMain->addWidget(m_btnMin); _vlayoutMain->addSpacing(10); _vlayoutMain->addWidget(m_btnMax); _vlayoutMain->addSpacing(10); _vlayoutMain->addWidget(m_btnClose); _vlayoutMain->setContentsMargins(10, 0, 10, 0); setLayout(_vlayoutMain); connect(m_btnMin, &QPushButton::clicked, this, &uititlebar::on_btn_clicked); connect(m_btnMax, &QPushButton::clicked, this, &uititlebar::on_btn_clicked); connect(m_btnClose, &QPushButton::clicked, this, &uititlebar::on_btn_clicked); } void uititlebar::updateMaximize() { QWidget* pWindow = this->window(); if (pWindow->isTopLevel()) { bool bMaximize = pWindow->isMaximized(); m_btnMax->setToolTip(bMaximize ? tr("还原") : tr("最大化")); m_btnMax->setProperty("maximizeProperty", bMaximize ? "restore" : "maximize"); // 手动更新样式 m_btnMax->style()->unpolish(m_btnMax); m_btnMax->style()->polish(m_btnMax); m_btnMax->update(); m_btnMax->setStyle(QApplication::style()); } } void uititlebar::on_btn_clicked() { QPushButton* _btn = qobject_cast<QPushButton*>(sender()); QWidget* _wnd = this->window(); if (_wnd->isTopLevel()) { if (_btn == m_btnMin) { _wnd->showMinimized(); } else if (_btn == m_btnMax) { if (_wnd->isMaximized()) _wnd->showNormal(); else _wnd->showMaximized(); updateMaximize(); } else if (_btn == m_btnClose) qApp->quit(); } } void uititlebar::mousePressEvent(QMouseEvent* event) { m_isPressed = true; m_startMovePos = event->globalPos(); return QWidget::mousePressEvent(event); } void uititlebar::mouseMoveEvent(QMouseEvent* event) { if (m_isPressed) { QWidget* pWindow = this->window(); if (pWindow->isMaximized()) { pWindow->showNormal(); updateMaximize(); } } if (m_isPressed) { QPoint movePoint = event->globalPos() - m_startMovePos; if (parentWidget()) { QPoint widgetPos = this->parentWidget()->pos(); m_startMovePos = event->globalPos(); parentWidget()->move(widgetPos.x() + movePoint.x(), widgetPos.y() + movePoint.y()); } } return QWidget::mouseMoveEvent(event); } void uititlebar::mouseReleaseEvent(QMouseEvent* event) { m_isPressed = false; return QWidget::mouseReleaseEvent(event); } void uititlebar::mouseDoubleClickEvent(QMouseEvent* event) { // 双击时获取焦点 this->setFocus(); QWidget* pWindow = this->window(); if (pWindow->isMaximized()) pWindow->showNormal(); else pWindow->showMaximized(); updateMaximize(); }
#pragma once #include <QWidget> class uicontentwnd : public QWidget { Q_OBJECT Q_PROPERTY(QPixmap background_image READ getImage WRITE setImage) public: uicontentwnd(QWidget *parent = Q_NULLPTR); ~uicontentwnd(); protected: void paintEvent(QPaintEvent* ev) override; private: const QPixmap& getImage() { return m_pixmapBk; } void setImage(const QPixmap& _pixmap); private: QPixmap m_pixmapBk; }; ```cpp #include "stdafx.h" #include "uicontentwnd.h" uicontentwnd::uicontentwnd(QWidget *parent) : QWidget(parent) { } uicontentwnd::~uicontentwnd() { } void uicontentwnd::setImage(const QPixmap& _pixmap) { m_pixmapBk = _pixmap; update(); } void uicontentwnd::paintEvent(QPaintEvent* ev) { if (!m_pixmapBk.isNull()) {// 背景图保持 居中等比例缩放 QPainter p(this); QPixmap pix = m_pixmapBk.scaled(rect().size(), Qt::KeepAspectRatioByExpanding, Qt::FastTransformation); double pixwidth = static_cast<double>(pix.width()); double pixheight = static_cast<double>(pix.height()); double showwidth = static_cast<double>(width()); double showheight = static_cast<double>(height()); QRect midrect( (showwidth / 2.0 - pixwidth / 2.0), (showheight / 2.0 - pixheight / 2.0), pixwidth, pixheight); p.drawPixmap(midrect, pix); } }
#pragma once
#include <QWidget>
class uimainwnd : public QMainWindow
{
Q_OBJECT
public:
uimainwnd(QWidget *parent = Q_NULLPTR);
~uimainwnd();
private:
void initUI();
};
#include "stdafx.h" #include "uimainwnd.h" #include "uititlebar.h" #include "uicontentwnd.h" uimainwnd::uimainwnd(QWidget *parent) : QMainWindow(parent) { initUI(); } uimainwnd::~uimainwnd() { } void uimainwnd::initUI() { resize(1280, 720); setWindowFlags(Qt::CustomizeWindowHint); setMouseTracking(true); uititlebar *_uititlebar = new uititlebar(this); setMenuWidget(_uititlebar); uicontentwnd *_uicontentwnd = new uicontentwnd(this); this->setCentralWidget(_uicontentwnd); }
uititlebar QLabel#logoLabel { border-image: url(:/MyQtTest/live); } /*关闭*/ uititlebar QPushButton#closeButton { background-color: transparent; border-style: none; outline: none; border-image: url(:/MyQtTest/btn_close_normal); } uititlebar QPushButton#closeButton:hover { border-image: url(:/MyQtTest/btn_close_hover); } uititlebar QPushButton#closeButton:pressed { border-image: url(:/MyQtTest/btn_close_press); } /*最大化*/ uititlebar QPushButton#maxButton { background-color: transparent; border-style: none; outline: none; border-image: url(:/MyQtTest/btn_max_normal); } uititlebar QPushButton#maxButton:hover { border-image: url(:/MyQtTest/btn_max_hover); } uititlebar QPushButton#maxButton:pressed { border-image: url(:/MyQtTest/btn_max_press); } uititlebar QPushButton#maxButton[maximizeProperty="maximize"] { background-color: transparent; border-style: none; outline: none; border-image: url(:/MyQtTest/btn_max_normal); } uititlebar QPushButton#maxButton[maximizeProperty="maximize"]:hover { border-image: url(:/MyQtTest/btn_max_hover); } uititlebar QPushButton#maxButton[maximizeProperty="maximize"]:pressed { border-image: url(:/MyQtTest/btn_max_press); } uititlebar QPushButton#maxButton[maximizeProperty="restore"] { background-color: transparent; border-style: none; outline: none; border-image: url(:/MyQtTest/abtn_max_normal); } uititlebar QPushButton#maxButton[maximizeProperty="restore"]:hover { border-image: url(:/MyQtTest/abtn_max_hover); } uititlebar QPushButton#maxButton[maximizeProperty="restore"]:pressed { border-image: url(:/MyQtTest/abtn_max_press); } /*最小化*/ uititlebar QPushButton#minButton { background-color: transparent; border-style: none; outline: none; border-image: url(:/MyQtTest/btn_min_normal); } uititlebar QPushButton#minButton:hover { border-image: url(:/MyQtTest/btn_min_hover); } uititlebar QPushButton#minButton:pressed { border-image: url(:/MyQtTest/btn_min_press); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。