Implement a Qt program with an image border as the window border. You can drag it with the left mouse button and right-click it to launch the program.
This program has three files: shapewidget. h, shapewidget. cpp, and main. cpp.
Custom header file: shapewidget. h
# Ifndef SHAPEWIDGET_H
# Define SHAPEWIDGET_H
# Include <QtGui>
Class QPoint; // define a Qpoint class
Class ShapeWidget: public QWidget // ShaoeWidget inherits QWidget
{
Q_OBJECT
Public:
ShapeWidget (QWidget * parent = 0); // create a constructor with empty content
Protected:
Void mousePressEvent (QMouseEvent *); // you can specify a mouse-Click Event function.
Void mouseMoveEvent (QMouseEvent *); // you can call this function to customize a mouse drag event.
Void paintEvent (QPaintEvent *); // customizes a screen flushing event Function
Private:
QPoint dragPosition; // defines the member variable of a QPoint.
};
# Endif
Implementation file: shapewidget. cpp
# Include "shapewidget. h"
ShapeWidget: ShapeWidget (QWidget * parent) // external rewriting Constructor
: QWidget (parent, Qt: FramelessWindowHint) // initialize the parameter type
{
QPixmap pix; // set a QPixmap object.
Pix. load (":/images/Watermelon.png", 0, Qt: AvoidDither | Qt: ThresholdDither | Qt: ThresholdAlphaDither );
Resize (pix. size (); // set the window size to the image size.
SetMask (pix. mask (); // filter the transparent part of the image by using the pix method and use it as the irregular border of the shapeWidget.
}
Void ShapeWidget: mousePressEvent (QMouseEvent * event)
{
If (event-> button () = Qt: LeftButton) // CLICK THE LEFT MOUSE
{
DragPosition = event-> globalPos ()-frameGeometry (). topLeft ();
// GlobalPos () obtains the relative path of the root window, And frameGeometry (). topLeft () obtains the position in the upper left corner of the main window.
Event-> accept (); // The mouse event is received by the system.
}
If (event-> button () = Qt: RightButton)
{
Close ();
}
}
Void ShapeWidget: mouseMoveEvent (QMouseEvent * event)
{
If (event-> buttons () = Qt: LeftButton) // when the left mouse button is clicked.
{
Move (event-> globalPos ()-dragPosition); // move the window
Event-> accept ();
}
}
Void ShapeWidget: paintEvent (QPaintEvent *)
{
QPainter painter (this); // create a QPainter object
Painter. drawPixmap (, QPixmap (":/images/Watermelon.png"); // draw an image to the window
/*
QPixmap (":/images/Watermelon.png") if it is changed to QPixmap (), you can only see the drawn frame. You cannot see the image color, that is, you cannot see the image.
*/
}
Main function: main. cpp
# Include <QApplication>
# Include "shapewidget. h"
Int
Main (int argc, char * argv [])
{
QApplication app (argc, argv );
ShapeWidget shape;
Shape. show ();
Return app.exe c ();
}
Implement a Qt program with an image border as the window border. You can drag it with the left mouse button and right-click it to launch the program.
This program has three files: shapewidget. h, shapewidget. cpp, and main. cpp.
Custom header file: shapewidget. h
# Ifndef SHAPEWIDGET_H
# Define SHAPEWIDGET_H
# Include <QtGui>
Class QPoint; // define a Qpoint class
Class ShapeWidget: public QWidget // ShaoeWidget inherits QWidget
{
Q_OBJECT
Public:
ShapeWidget (QWidget * parent = 0); // create a constructor with empty content
Protected:
Void mousePressEvent (QMouseEvent *); // you can specify a mouse-Click Event function.
Void mouseMoveEvent (QMouseEvent *); // you can call this function to customize a mouse drag event.
Void paintEvent (QPaintEvent *); // customizes a screen flushing event Function
Private:
QPoint dragPosition; // defines the member variable of a QPoint.
};
# Endif
Implementation file: shapewidget. cpp
# Include "shapewidget. h"
ShapeWidget: ShapeWidget (QWidget * parent) // external rewriting Constructor
: QWidget (parent, Qt: FramelessWindowHint) // initialize the parameter type
{
QPixmap pix; // set a QPixmap object.
Pix. load (":/images/Watermelon.png", 0, Qt: AvoidDither | Qt: ThresholdDither | Qt: ThresholdAlphaDither );
Resize (pix. size (); // set the window size to the image size.
SetMask (pix. mask (); // filter the transparent part of the image by using the pix method and use it as the irregular border of the shapeWidget.
}
Void ShapeWidget: mousePressEvent (QMouseEvent * event)
{
If (event-> button () = Qt: LeftButton) // CLICK THE LEFT MOUSE
{
DragPosition = event-> globalPos ()-frameGeometry (). topLeft ();
// GlobalPos () obtains the relative path of the root window, And frameGeometry (). topLeft () obtains the position in the upper left corner of the main window.
Event-> accept (); // The mouse event is received by the system.
}
If (event-> button () = Qt: RightButton)
{
Close ();
}
}
Void ShapeWidget: mouseMoveEvent (QMouseEvent * event)
{
If (event-> buttons () = Qt: LeftButton) // when the left mouse button is clicked.
{
Move (event-> globalPos ()-dragPosition); // move the window
Event-> accept ();
}
}
Void ShapeWidget: paintEvent (QPaintEvent *)
{
QPainter painter (this); // create a QPainter object
Painter. drawPixmap (, QPixmap (":/images/Watermelon.png"); // draw an image to the window
/*
QPixmap (":/images/Watermelon.png") if it is changed to QPixmap (), you can only see the drawn frame. You cannot see the image color, that is, you cannot see the image.
*/
}
Main function: main. cpp
# Include <QApplication>
# Include "shapewidget. h"
Int
Main (int argc, char * argv [])
{
QApplication app (argc, argv );
ShapeWidget shape;
Shape. show ();
Return app.exe c ();
}