#include <QBitmap>
#include <QPainter>
#include <QEvent>
#include <QDebug>
#include "MainWindow.h"
bool dragged = false;
{
setupUi(this);
setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
setWindowTitle(tr("Fancy Widget"));
setMouseTracking(true);
setStyleSheet(
"* {color: qlineargradient(spread:pad, x1:0 y1:0, x2:1 y2:0, stop:0 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255));"
"background: qlineargradient(x1:0 y1:0, x2:0 y2:1, stop:0 cyan, stop:1 blue);}");
QString strMinimize
= "QPushButton{border-image: url(:/images/img_TitleMinimize.png)}" "QPushButton:hover { border-image: url(:/images/img_TitleMinimizeHover.png)}"
"QPushButton:pressed { border-image: url(:/images/img_TitleMinimizePressed.png);}";
MinButton->setStyleSheet(strMinimize);
QString strMaximize
= "QPushButton{border-image: url(:/images/img_TitleMaximize.png)}" "QPushButton:hover { border-image: url(:/images/img_TitleMaximizeHover.png)}"
"QPushButton:pressed { border-image: url(:/images/img_TitleMaximizePressed.png);}";
MaxButton->setStyleSheet(strMaximize);
QString strClose
= "QPushButton{border-image: url(:/images/img_TitleClose.png)}" "QPushButton:hover { border-image: url(:/images/img_TitleCloseHover.png)}"
"QPushButton:pressed { border-image: url(:/images/img_TitleClosePressed.png);}";
CloseButton->setStyleSheet(strClose);
m_pCustomFrameWidget = new CustomFrameWidget();
connect( MinButton, SIGNAL( clicked() ), this, SLOT( OnMinimized() ) );
connect( CloseButton, SIGNAL( clicked() ), this, SLOT( close() ) );
}
void MainWindow::MoveCustomFrameWidget()
{
m_pCustomFrameWidget->move(this->pos().x() - 40, this->pos().y() + 40);
m_pCustomFrameWidget->show();
}
void MainWindow::OnMinimized()
{
m_pCustomFrameWidget->showMinimized();
this->showMinimized();
}
{
if(dragged && event->buttons() & Qt::LeftButton)
{
move(event->globalPos()-m_dragPosition);
m_pCustomFrameWidget->move((event->globalPos() - m_dragPosition).x() - 40, (event->globalPos() - m_dragPosition).y() + 40);
event->accept();
}
}
{
if(!dragged && event->button() == Qt::LeftButton)
{
if(IsPointInCaption(event->pos()))
{
m_dragPosition = event->pos();
}
}
dragged = false;
}
bool MainWindow
::IsPointInCaption(const QPoint & point
) {
QRect captionRect
(0,
0,
800,
30);
return (captionRect.contains(point));
}
{
if(event->button() == Qt::LeftButton)
{
dragged = false;
QPoint pos
= event
->globalPos
();
if(IsPointInCaption(event->pos()))
{
dragged = true;
m_dragPosition = pos - frameGeometry().topLeft();
}
}
}
{
m_pCustomFrameWidget->close();
}
{
m_pCustomFrameWidget->showNormal();
}
{
m_pCustomFrameWidget->hide();
}
bool MainWindow
::event( QEvent * pEvent
) {
if ( QEvent::WindowActivate == pEvent
->type
() ) m_pCustomFrameWidget->raise();
return bResult;
}
CustomFrameWidget
::CustomFrameWidget(QWidget* parent
) :QDialog(parent
){
setupUi(this);
setWindowFlags(Qt::Window | Qt::FramelessWindowHint );
frame->setStyleSheet(
"* {color: qlineargradient(spread:pad, x1:0 y1:0, x2:1 y2:0, stop:0 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255));"
"background: qlineargradient( x1:0 y1:0, x2:1 y2:0, stop:0 green, stop:1 darkMagenta);}");
this->setAttribute(Qt::WA_TranslucentBackground, true);
}
#include <QBitmap>
#include <QPainter>
#include <QEvent>
#include <QDebug>
#include "MainWindow.h"
bool dragged = false;
MainWindow::MainWindow(QWidget *parent):QMainWindow(parent)
{
setupUi(this);
setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
setWindowTitle(tr("Fancy Widget"));
setMouseTracking(true);
setStyleSheet(
"* {color: qlineargradient(spread:pad, x1:0 y1:0, x2:1 y2:0, stop:0 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255));"
"background: qlineargradient(x1:0 y1:0, x2:0 y2:1, stop:0 cyan, stop:1 blue);}");
QString strMinimize = "QPushButton{border-image: url(:/images/img_TitleMinimize.png)}"
"QPushButton:hover { border-image: url(:/images/img_TitleMinimizeHover.png)}"
"QPushButton:pressed { border-image: url(:/images/img_TitleMinimizePressed.png);}";
MinButton->setStyleSheet(strMinimize);
QString strMaximize = "QPushButton{border-image: url(:/images/img_TitleMaximize.png)}"
"QPushButton:hover { border-image: url(:/images/img_TitleMaximizeHover.png)}"
"QPushButton:pressed { border-image: url(:/images/img_TitleMaximizePressed.png);}";
MaxButton->setStyleSheet(strMaximize);
QString strClose = "QPushButton{border-image: url(:/images/img_TitleClose.png)}"
"QPushButton:hover { border-image: url(:/images/img_TitleCloseHover.png)}"
"QPushButton:pressed { border-image: url(:/images/img_TitleClosePressed.png);}";
CloseButton->setStyleSheet(strClose);
m_pCustomFrameWidget = new CustomFrameWidget();
connect( MinButton, SIGNAL( clicked() ), this, SLOT( OnMinimized() ) );
connect( CloseButton, SIGNAL( clicked() ), this, SLOT( close() ) );
}
void MainWindow::MoveCustomFrameWidget()
{
m_pCustomFrameWidget->move(this->pos().x() - 40, this->pos().y() + 40);
m_pCustomFrameWidget->show();
}
void MainWindow::OnMinimized()
{
m_pCustomFrameWidget->showMinimized();
this->showMinimized();
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
QMainWindow::mouseMoveEvent(event);
if(dragged && event->buttons() & Qt::LeftButton)
{
move(event->globalPos()-m_dragPosition);
m_pCustomFrameWidget->move((event->globalPos() - m_dragPosition).x() - 40, (event->globalPos() - m_dragPosition).y() + 40);
event->accept();
}
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
QMainWindow::mouseReleaseEvent(event);
if(!dragged && event->button() == Qt::LeftButton)
{
if(IsPointInCaption(event->pos()))
{
m_dragPosition = event->pos();
}
}
dragged = false;
}
bool MainWindow::IsPointInCaption(const QPoint & point)
{
QRect captionRect(0,0,800,30);
return (captionRect.contains(point));
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton)
{
dragged = false;
QPoint pos = event->globalPos();
if(IsPointInCaption(event->pos()))
{
dragged = true;
m_dragPosition = pos - frameGeometry().topLeft();
}
}
QMainWindow::mousePressEvent(event);
}
void MainWindow::closeEvent( QCloseEvent * pEvent )
{
m_pCustomFrameWidget->close();
}
void MainWindow::showEvent( QShowEvent * pEvent )
{
m_pCustomFrameWidget->showNormal();
}
void MainWindow::hideEvent( QHideEvent * pEvent )
{
m_pCustomFrameWidget->hide();
}
bool MainWindow::event( QEvent * pEvent )
{
bool bResult = QMainWindow::event( pEvent );
if ( QEvent::WindowActivate == pEvent->type() )
m_pCustomFrameWidget->raise();
return bResult;
}
CustomFrameWidget::CustomFrameWidget(QWidget* parent) :QDialog(parent)
{
setupUi(this);
setWindowFlags(Qt::Window | Qt::FramelessWindowHint );
frame->setStyleSheet(
"* {color: qlineargradient(spread:pad, x1:0 y1:0, x2:1 y2:0, stop:0 rgba(0, 0, 0, 255), stop:1 rgba(255, 255, 255, 255));"
"background: qlineargradient( x1:0 y1:0, x2:1 y2:0, stop:0 green, stop:1 darkMagenta);}");
this->setAttribute(Qt::WA_TranslucentBackground, true);
}
To copy to clipboard, switch view to plain text mode
void MainWindow
::attachWidget( QWidget * pWidget,
const QPos
& relativePos
) {
AttachedWidget attached;
attached.mpWidget = pWidget;
attached.mRelativePos = relativePos;
mAttachedWidgets.push_back( attached );
}
// and change the methods:
{
for ( attachedWidget : mAttachedWidgets )
attachedWidget.mpWidget->close();
}
// and so forth for the other events
void MainWindow::attachWidget( QWidget * pWidget, const QPos & relativePos )
{
AttachedWidget attached;
attached.mpWidget = pWidget;
attached.mRelativePos = relativePos;
mAttachedWidgets.push_back( attached );
}
// and change the methods:
void MainWindow::closeEvent( QCloseEvent * pEvent )
{
for ( attachedWidget : mAttachedWidgets )
attachedWidget.mpWidget->close();
}
// and so forth for the other events
To copy to clipboard, switch view to plain text mode
Bookmarks