A borderless Qt instance

Source: Internet
Author: User

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 ();
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.