titlebar.h
| Код | #ifndef TITLEBAR_H #define TITLEBAR_H
#include <QToolButton> #include <QLayout> #include <QStyle> #include <QLabel> #include <QMouseEvent> #include <QPushButton>
class TitleBar : public QWidget { Q_OBJECT
public: TitleBar(QWidget *parent); QLabel *label;
public slots: void showSmall(); void showMaxRestore();
protected: void mousePressEvent(QMouseEvent *me); void mouseMoveEvent(QMouseEvent *me);
private: QPushButton *minimize; QPushButton *maximize; QPushButton *close; QPixmap restorePix, maxPix; bool maxNormal; QPoint startPos; QPoint clickPos; };
#endif // TITLEBAR_H
|
titlebar.cpp
| Код | #include "titlebar.h"
TitleBar::TitleBar(QWidget *parent){ // Don't let this widget inherit the parent's background color setAutoFillBackground(true); // Use a brush with a Highlight color role to render the background setBackgroundRole(QPalette::Highlight);
minimize = new QPushButton(this); maximize = new QPushButton(this); QLabel *space=new QLabel(""); space->setFixedWidth(5); close= new QPushButton(this);
// Use the style to set the button pixmaps
QPixmap pix; pix.load(":/image/close.png"); close->setIcon(pix); close->setIconSize(QSize(42, 17));
maxPix.load(":/image/maximize.png"); maximize->setIcon(maxPix); maximize->setIconSize(QSize(24, 17));
pix.load(":/image/minimize.png"); minimize->setIcon(pix); minimize->setIconSize(QSize(24, 17));
restorePix.load(":/image/restore.png");
minimize->setFixedSize(24, 17); close->setFixedSize(42, 17); maximize->setFixedSize(24, 17);
label = new QLabel(this); label->setText("Window Title"); parent->setWindowTitle("Window Title");
QHBoxLayout *hbox = new QHBoxLayout(this);
hbox->addWidget(label); hbox->addWidget(minimize); hbox->addWidget(maximize); hbox->addWidget(space); hbox->addWidget(close); hbox->setContentsMargins(2,2,2,2);
hbox->insertStretch(1, 500); hbox->setSpacing(0); setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
maxNormal = false;
connect(close, SIGNAL( clicked() ), parent, SLOT(close() ) ); connect(minimize, SIGNAL( clicked() ), this, SLOT(showSmall() ) ); connect(maximize, SIGNAL( clicked() ), this, SLOT(showMaxRestore() ) ); }
void TitleBar::showSmall(){ parentWidget()->showMinimized(); }
void TitleBar::showMaxRestore(){ if (maxNormal){ parentWidget()->showNormal(); maxNormal = !maxNormal; maximize->setIcon(maxPix); }else{ parentWidget()->showMaximized(); maxNormal = !maxNormal; maximize->setIcon(restorePix); } }
void TitleBar::mousePressEvent(QMouseEvent *me){ startPos = me->globalPos(); clickPos = mapToParent(me->pos()); }
void TitleBar::mouseMoveEvent(QMouseEvent *me){ if (maxNormal) return; parentWidget()->move(me->globalPos() - clickPos); }
|
frame.h
| Код | #ifndef FRAME_H #define FRAME_H
#include <QFrame> #include <QPoint> #include <QMouseEvent> #include <QMainWindow>
#include "titlebar.h"
class Frame : public QFrame { public: Frame(QMainWindow &win); QWidget *contentWidget() const; TitleBar *titleBar() const; void mousePressEvent(QMouseEvent *e); void mouseMoveEvent(QMouseEvent *e); void mouseReleaseEvent(QMouseEvent *e); TitleBar *m_titleBar;
private: QWidget *m_content; QPoint m_old_pos; bool m_mouse_down; bool left, right, bottom; };
#endif // FRAME_H
|
frame.cpp
| Код | #include "frame.h" #include "mainwindow.h" #include "ui_mainwindow.h"
Frame::Frame(QMainWindow &win) { m_mouse_down = false; setFrameShape(Panel);
// Make this a borderless window which can't // be resized or moved via the window system setWindowFlags(Qt::FramelessWindowHint); this->setMouseTracking(true);
m_titleBar = new TitleBar(this);
m_content = win.centralWidget();
QVBoxLayout *vbox = new QVBoxLayout(this); vbox->addWidget(m_titleBar); vbox->setMargin(0); vbox->setSpacing(0);
QVBoxLayout *layout = new QVBoxLayout(this); win.menuBar()->setFixedHeight(19); layout->addWidget(win.menuBar()); layout->addWidget(win.centralWidget()); win.statusBar()->setFixedHeight(19); layout->addWidget(win.statusBar()); layout->setMargin(0); layout->setSpacing(0); vbox->addLayout(layout); }
// Allows you to access the content area of the frame // where widgets and layouts can be added QWidget *Frame::contentWidget() const{ return m_content; }
TitleBar *Frame::titleBar() const{ return m_titleBar; }
void Frame::mousePressEvent(QMouseEvent *e){ m_old_pos = e->pos(); m_mouse_down = e->button() == Qt::LeftButton; }
void Frame::mouseMoveEvent(QMouseEvent *e){
int x = e->x(); int y = e->y();
if (m_mouse_down){ int dx = x - m_old_pos.x(); int dy = y - m_old_pos.y();
QRect g = geometry();
if (left) g.setLeft(g.left() + dx); if (right) g.setRight(g.right() + dx); if (bottom) g.setBottom(g.bottom() + dy);
setGeometry(g);
m_old_pos = QPoint(!left ? e->x() : m_old_pos.x(), e->y()); }else{ QRect r = rect(); left = qAbs(x - r.left()) <= 5; right = qAbs(x - r.right()) <= 5; bottom = qAbs(y - r.bottom()) <= 5; bool hor = left | right;
if (hor && bottom){ if (left) setCursor(Qt::SizeBDiagCursor); else setCursor(Qt::SizeFDiagCursor); }else if (hor){ setCursor(Qt::SizeHorCursor); }else if (bottom){ setCursor(Qt::SizeVerCursor); }else{ setCursor(Qt::ArrowCursor); } } }
void Frame::mouseReleaseEvent(QMouseEvent *e){ m_mouse_down = false; }
|
mainwindow.h
| Код | #ifndef MAINWINDOW_H #define MAINWINDOW_H
#include <QtGui/QMainWindow> #include "titlebar.h" #include "frame.h"
#include <QApplication> #include <QtGui> #include <QLayout> #include <QStyle> #include <QVBoxLayout>
namespace Ui { class MainWindow; }
class MainWindow : public QMainWindow { Q_OBJECT
public: MainWindow(QWidget *parent = 0); ~MainWindow();
private: Ui::MainWindow *ui; };
#endif // MAINWINDOW_H
|
mainwindow.cpp
| Код | #include "mainwindow.h" #include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
}
MainWindow::~MainWindow() { delete ui; }
|
main.cpp
| Код | #include <QtGui/QApplication> #include "mainwindow.h" #include <QLabel>
int main(int argc, char **argv) {
QApplication a(argc, argv);
MainWindow w; Frame box(w); box.move(100,50); box.m_titleBar->label->setText("My Window Title");
box.resize(w.width(), w.height()+10); box.show(); return a.exec(); }
|
res.qrc
| Код | <RCC> <qresource prefix="/image" > <file>restore.png</file> <file>close.png</file> <file>maximize.png</file> <file>minimize.png</file> </qresource> </RCC>
|
|