#ifndef TITLEBAR_H#define TITLEBAR_H#include <QtGui/QLabel>QT_BEGIN_NAMESPACEclass QPushButton;class QPixmap;class QPoint;QT_END_NAMESPACEclass TitleBar : public QLabel{ Q_OBJECTprivate: QPushButton* _minimizeButton, * _closeButton; QPixmap _titleBarPixmap; QPoint startPos; bool _isPressed; QWidget* contentWidget;protected: void mousePressEvent(QMouseEvent* ); void mouseReleaseEvent(QMouseEvent* ); void mouseMoveEvent(QMouseEvent* );public: TitleBar(QWidget * contentWidget); ~TitleBar();public slots: void show(); void move(int x, int y); void close(); void showMinimized();};#endif // TITLEBAR_H
#include "titlebar.h"QT_BEGIN_HEADER#include <QPushButton>#include <QPainter>#include <QImage>#include <QHBoxLayout>#include <QMouseEvent>#include <QPoint>#include <QApplication>#include <QPixmap>#include <QBitmap>#include <QShowEvent>QT_END_HEADERTitleBar::TitleBar(QWidget* contentWidget) : _titleBarPixmap(":/images/title_bar.png"){ _isPressed = false; _closeButton = new QPushButton; _minimizeButton = new QPushButton; QString qss = ""; qss.append("QPushButton { border: none; background-image: url(:images/button/window/line.png); background-position: center center; background-repeat: none; height: 16px; width: 16px; }"); qss.append("QPushButton:hover { border: none; background-image: url(:images/button/window/line_hover.png); background-position: center center; background-repeat: none; height: 16px; width: 16px; }"); qss.append("QPushButton:pressed { border: none; background-image: url(:images/button/window/line_active.png); background-position: center center; background-repeat: none; height: 16px; width: 16px; }"); _minimizeButton->setStyleSheet(qss); qss = ""; qss.append("QPushButton { border: none; background-image: url(:images/button/window/close.png); background-position: center center; background-repeat: none; height: 16px; width: 16px; }"); qss.append("QPushButton:hover { border: none; background-image: url(:images/button/window/close_hover.png); background-position: center center; background-repeat: none; height: 16px; width: 16px; }"); qss.append("QPushButton:pressed { border: none; background-image: url(:images/button/window/close_active.png); background-position: center center; background-repeat: none; height: 16px; width: 16px; }"); _closeButton->setStyleSheet(qss); QHBoxLayout* layout = new QHBoxLayout; layout->addStretch(); layout->addWidget(_minimizeButton); layout->addWidget(_closeButton); layout->addSpacing(10); setLayout(layout); setFixedHeight(48); setFixedWidth(368); this->contentWidget = contentWidget; this->contentWidget->setWindowFlags(Qt::FramelessWindowHint | Qt::Drawer); connect(_closeButton, SIGNAL(clicked()), this, SLOT(close())); connect(_minimizeButton, SIGNAL(clicked()), this, SLOT(showMinimized())); setPixmap(_titleBarPixmap); setMask(_titleBarPixmap.mask());}TitleBar::~TitleBar(){}void TitleBar::mousePressEvent(QMouseEvent* event){ if (event->button() == Qt::LeftButton) { _isPressed = true; startPos = event->pos(); }}void TitleBar::mouseReleaseEvent(QMouseEvent* event){ if(event->button() == Qt::LeftButton) { _isPressed = false; }}void TitleBar::mouseMoveEvent(QMouseEvent* event){ if (_isPressed) { move(event->globalX() - startPos.x(), event->globalY() - startPos.y()); }}void TitleBar::show(){ QWidget::show(); contentWidget->show(); contentWidget->move(x(), y() + height());}void TitleBar::move(int x, int y){ QWidget::move(x, y); contentWidget->move(x, y + height());}void TitleBar::close(){ QWidget::close(); contentWidget->close();}void TitleBar::showMinimized(){ QWidget::showMinimized();}