Here the custom window is inherited from the Qframe, the interior is divided into two parts, the upper part is the title bar, the following is the specific contents of the window. The upper and lower sections are fixed by layout. The final window of various mouse drag, zoom, and other actions by adding mouse events to complete a window of the general function. Of course, this title bar can be placed anywhere by changing the layout.
Here's the code for the implementation:
#pragma once
#ifndef CUSTOMWIDGET_H_
#define CUSTOMWIDGET_H_
#include <QtWidgets/QFrame>
Class Qtoolbutton;
Class Customwidget:public Qframe
{
Q_OBJECT
Public
explicit CustomWidget();~CustomWidget();void setContentWidget(QWidget *contentWidget);void setTitleInfo(const QString &titleString, const QPixmap &windowIcon);
Public Slots:
void slotShowSmall();void slotShowMaxRestore();void onMoveWindow();
Signals:
void moveWindow();
Protected
void mousePressEvent(QMouseEvent *);void mouseMoveEvent(QMouseEvent *);void mouseReleaseEvent(QMouseEvent *);bool nativeEvent(const QByteArray & eventType, void * message, long * result);void paintEvent(QPaintEvent *);
Private
bool isMax;bool isPress;QPoint startPos;QPoint clickPos;QWidget *m_pTitleWidget;//顶栏窗体QWidget *m_pContentWidget;//内容窗体QPixmap maxPixmap;QPixmap restorePixmap;QToolButton *m_pMaxButton;
};
#endif //!CUSTOMWIDGET_H_
CustomWidget.cpp:
#include "stdafx.h"
#include "customwidget.h"
#include <QtWidgets/QLabel>
#include <QtWidgets/QToolButton>
#include <QtWidgets/QHBoxLayout>
#include <QtCore/QEvent>
#include <Window.h>
static const int TITLE_HEIGHT = 40;//标题栏高度
static const int FRAME_BORDER = 2;//边框宽度
CustomWidget::CustomWidget()
: m_pContentWidget(0)
//, m_llLocalWinId(0)
//, m_llRemoteWinId(0)
{
this->setWindowFlags(Qt::FramelessWindowHint);this->setAttribute(Qt::WA_TranslucentBackground);this->setMouseTracking(true);isMax = false;isPress = false;m_pTitleWidget = new QWidget();
}
Customwidget::~customwidget ()
{
}
void Customwidget::setcontentwidget (Qwidget *contentwidget)
{
if (contentWidget){ m_pContentWidget = contentWidget; m_pContentWidget->move(m_pTitleWidget->rect().bottomLeft()); m_pContentWidget->show(); resize(m_pTitleWidget->width(), m_pTitleWidget->height() + m_pContentWidget->height());}
}
void Customwidget::settitleinfo (const QString &titlestring, const QPIXMAP &windowicon)
{
Qlabel *logolabel = new Qlabel (); Logolabel->setpixmap (Windowicon); logolabel->setfixedsize (+); logoLabel- >setscaledcontents (TRUE); Qlabel *titlelabel = new Qlabel (); Titlelabel->settext (titlestring); Qfont Titlefont = Titlelabel->font (); Titlefont.setbold (true); Titlelabel->setfont (Titlefont);titleLabel-> Setobjectname ("Whitelabel"); Titlelabel->setstylesheet ("color:white;"); Qtoolbutton *minbutton = new Qtoolbutton (), Connect (Minbutton, SIGNAL ()), this, SLOT (clicked ())); Slotshowsmall Pmaxbutton = new Qtoolbutton (), Connect (M_pmaxbutton, SIGNAL (clicked ()), this, SLOT (Slotshowmaxrestore ())); Qtoolbutton *closebutton = new Qtoolbutton (), Connect (CloseButton, SIGNAL ()), this, SLOT (Close ())), Connect ( This, SIGNAL (MoveWindow ()), this, SLOT (Onmovewindow ())); Qhboxlayout *titlelayout = new Qhboxlayout (); Titlelayout->addwidget (Logolabel); Titlelayout->addwidget ( Titlelabel); Titlelabel->setcontentsmargins (5, 0, 0, 0); Titlelayout->addstretch (); Titlelayout->addWidgets (Minbutton, 0, Qt::aligntop), Titlelayout->addwidget (M_pmaxbutton, 0, Qt::aligntop);titlelayout-> AddWidget (CloseButton, 0, Qt::aligntop); titlelayout->setspacing (0); Titlelayout->setcontentsmargins (5, 0, 0, 0 ); M_ptitlewidget->setfixedsize (title_height); M_ptitlewidget->setlayout (titlelayout); m_pTitleWidget- >installeventfilter (0); M_ptitlewidget->setautofillbackground (true); Qpalette Palette;palette.setcolor (Qpalette::background, Qcolor (31,25,25)); M_ptitlewidget->setpalette (palette) ;
}
void Customwidget::slotshowsmall ()//Minimized
{
This->showminimized ();
}
void Customwidget::slotshowmaxrestore ()//Restore button
{
if (isMax) { this->showNormal(); m_pMaxButton->setIcon(maxPixmap);}else { this->showMaximized(); m_pMaxButton->setIcon(restorePixmap);}isMax = !isMax;
}
void Customwidget::onmovewindow ()
{
M_pcontentwidget->move (M_ptitlewidget->rect (). Bottomleft ());
}
void Customwidget::mousepressevent (Qmouseevent *e)
{
startPos = e->globalPos();clickPos = e->pos();if (e->button() == Qt::LeftButton) { if (e->type() == QEvent::MouseButtonPress) { isPress = true; } else if (e->type() == QEvent::MouseButtonDblClick && e->pos().y() <= TITLE_HEIGHT) { this->slotShowMaxRestore(); }}
}
void Customwidget::mousemoveevent (Qmouseevent *e)
{
if (isMax || !isPress) { return;}this->move(e->globalPos() - clickPos);emit moveWindow();
}
void Customwidget::mousereleaseevent (Qmouseevent *)
{
Ispress = false;
}
BOOL Customwidget::nativeevent (const Qbytearray & eventtype, void * message, long * result)//implement drag-and-drop border effect
{
Q_unused (eventtype); const int hit_border = 5;const MSG *msg = static_cast<msg*> (message); if (msg->message = = WM_ nchittest) {int xPos = ((int) (short) LoWord (Msg->lparam))-This->framegeometry (). x (); int yPos = ((int) (short) HiWord (Msg->lparam))-This->framegeometry (). Y (); if (This->childat (XPos, yPos) = = 0) {*result = htcaption; } else {return false; } if (XPos > 0 && xPos < hit_border) {*result = Htleft; } if (XPos > (This->width ()-Hit_border) && XPos < (this->width ()-0)) {*result = Htright; } if (YPos > 0 && yPos < hit_border) {*result = Httop; } if (YPos > (This->height ()-Hit_border) && YPos < (this->height ()-0)) {*result = Htbott OM; if (XPos > 0 && xPos < hit_border && yPos > 0 && yPos < hit_border) {*res Ult = Httopleft; } if (XPos > (this-≫width ()-Hit_border) && XPos < (this->width ()-0) && yPos > 0 && yPos < Hit_borde R) {*result = Httopright; if (XPos > 0 && xPos < hit_border && YPos > (this->height ()-Hit_border) && YPos < (this->height ()-0)) {*result = Htbottomleft; } if (XPos > (This->width ()-Hit_border) && XPos < (this->width ()-0) && YPos > (THIS-&G T;height ()-Hit_border) && YPos < (this->height ()-0)) {*result = Htbottomright; } return true; return false;
}
void Customwidget::p aintevent (qpaintevent *e)
{//Draw Border
int border = frame_border;if (this->ismaximized ()) {border = 0;} qpainter painter (this); Qpainterpath painterpath;painterpath.setfillrule (Qt::windingfill);p ainterpath.addrect (border, border, this-> Width ()-2 * border, This->height ()-2 * border);p Ainter.setrenderhint (qpainter::antialiasing, true); Painter.fillpath (Painterpath, Qbrush (Qt::black)); Qcolor color (n, a, N), for (int i = 0; i<border; i++) {Color.setalpha ((i + 1) * 30); Painter.setpen (color); Painter.drawrect (Border-i, Border-i, This->width ()-(border-i) * 2, This->height ()-(border-i) * 2);} Painter.setpen (Qt::nopen);p Ainter.setbrush (qt::black);//You can specify a title background picture in the Resource//painter.drawpixmap (Qrect (Border, Border, This->width () -2*border, This->height () -2*border), Qpixmap (Default_skin)); Painter.drawrect (Qrect (Border, Title_height, This->width ()-2 * border, this->height ()-Title_height-border)); Qframe::p aintevent (e);
}
End of full text
http://blog.csdn.net/u011915578/article/details/46924151
QT Custom Window (analog MainWindow)