Skip to content

Commit

Permalink
Add sample code for min/max/x & toolbar on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Verrify committed Jul 15, 2017
1 parent a401a81 commit 36c6451
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 6 deletions.
4 changes: 2 additions & 2 deletions TrueBorderlessWindow.pro → TrueFramelessWindow.pro
@@ -1,14 +1,14 @@
#-------------------------------------------------
#
# TrueBorderlessWindow.pro
# TrueFramelessWindow.pro
#
#-------------------------------------------------

QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = TrueBorderlessWindow
TARGET = TrueFramelessWindow
TEMPLATE = app

SOURCES += main.cpp\
Expand Down
2 changes: 1 addition & 1 deletion main.cpp
Expand Up @@ -3,7 +3,7 @@
#ifdef _WIN32
#include "QWinWidget.h"
#else
#include "Widget.h"
#include "widget.h"
#endif

#ifdef __APPLE__
Expand Down
78 changes: 77 additions & 1 deletion widget.cpp
@@ -1,7 +1,10 @@
#include "widget.h"

#include <QLabel>
#include <QLayout>

Widget::Widget(QWidget *parent)
: QWidget(parent)
: QMainWindow(parent)
{

//Set a black background for funsies
Expand All @@ -10,4 +13,77 @@ Widget::Widget(QWidget *parent)
setAutoFillBackground(true);
setPalette(Pal);







//Windows example of adding a toolbar + min/max/close buttons
#ifdef _WIN32

//Add the toolbar
toolBar = new QToolBar(this);
toolBar->setMovable(false);
toolBar->setFloatable(false);
addToolBar(toolBar);

//Create a transparent-to-mouse-events widget that pads right for a fixed width equivalent to min/max/close buttons
QWidget* btnSpacer = new QWidget(toolBar);
btnSpacer->setAttribute(Qt::WA_TransparentForMouseEvents);
btnSpacer->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
btnSpacer->setStyleSheet("background-color: none; border: none;");
btnSpacer->setFixedWidth(135 /* rough width of close/min/max buttons */);
toolBar->addWidget(btnSpacer);


//Create a title label just because
QLabel* titleLabel = new QLabel("TrueFramelessWindow");
titleLabel->setFixedWidth(160);

//Set it transparent to mouse events such that you can click and drag when moused over the label
titleLabel->setAttribute(Qt::WA_TransparentForMouseEvents);


//Create spacer widgets to keep the title centered
QWidget* leftSpacer = new QWidget(toolBar);
QWidget* rightSpacer = new QWidget(toolBar);

//Set them transparent to mouse events + auto-expanding in size
leftSpacer->setAttribute(Qt::WA_TransparentForMouseEvents);
leftSpacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
leftSpacer->setStyleSheet("background-color: none; border: none;");
rightSpacer->setAttribute(Qt::WA_TransparentForMouseEvents);
rightSpacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
rightSpacer->setStyleSheet("background-color: none; border: none;");

//Add spacers & title label
toolBar->addWidget(leftSpacer);
toolBar->addWidget(titleLabel);
toolBar->addWidget(rightSpacer);


//Create the min/max/close buttons
minimizeButton = new QPushButton("-");
maximizeButton = new QPushButton("O");
closeButton = new QPushButton("X");

maximizeButton->setCheckable(true);

minimizeButton->setFixedSize(45, 28);
maximizeButton->setFixedSize(45, 28);
closeButton->setFixedSize(45, 28);

toolBar->addWidget(minimizeButton);
toolBar->addWidget(maximizeButton);
toolBar->addWidget(closeButton);
toolBar->layout()->setAlignment(minimizeButton, Qt::AlignTop);
toolBar->layout()->setAlignment(maximizeButton, Qt::AlignTop);
toolBar->layout()->setAlignment(closeButton, Qt::AlignTop);

//An actual app should use icons for the buttons instead of text
//and style the different button states / widget margins in css

#endif

}
4 changes: 2 additions & 2 deletions widget.h
@@ -1,11 +1,11 @@
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QMainWindow>
#include <QPushButton>
#include <QToolBar>

class Widget : public QWidget
class Widget : public QMainWindow
{
Q_OBJECT
public:
Expand Down

0 comments on commit 36c6451

Please sign in to comment.