QT -- window setting for getting started

Posted by czambran on Tue, 19 May 2020 17:08:10 +0200

Header file

#Include < qicon > / / image loading
 #Include < qpalette > / / palette	
#Include < qbrush > / / brush
 #Include < qbitmap > / / used to remove the blank part of the picture

Set the icon and title of the window

//Set to the maximum window, do not change the window size
this->setFixedSize(481,579);      

//Set variable size window	 	
//  this->resize(481, 579);				

//Set window name
this->setWindowTitle("My window");

 //Set icon for window
this->setWindowIcon(QIcon("image/gameUi/AF2.png"));     

Set picture as window background

QPixmap pixmap_Background("image/gameUi/01.png");	//Image address

//Create palette
QPalette palette;   		

//Use brush to set background, target as background, use picture to fill
palette.setBrush(QPalette::Background,QBrush(QPixmap(pixmap_Background)));  

//Window application palette
this->setPalette(palette);     

//You can display the transparent part of the picture as a transparent header file < qbitmap > or < qtgui / qtgui >. If you want to use it, please see the settings window to move
//  this->setMask(pixmap_Background.mask());          

//Set the form auto fill background
this->setAutoFillBackground(true);                

Set window movement

this->setMask(pixmap_Background.mask());
Implementation effect: the outline of the window will be the same as that of your picture
Problem encountered: the closing and shrinking buttons of the window will be blocked and the window will not move
Solution: add buttons to control window closing and shrinking, and reload mouse events to control window movement


//Set up a QPoint to receive the points under the mouse
QPoint my_Point;

 //Reload mouse function - mouse down event
void mousePressEvent(QMouseEvent *event)
{
	//Read and click the coordinate point with mouse
	my_Point = event->globalPos();
}   

///Overloaded mouse function - mouse movement, can realize where you press the mouse to move, where the window moves
void mouseMoveEvent(QMouseEvent *event)
{
	//Record the moving points
	int dx = event->globalX() - my_Point.x();
	int dy = event->globalY() - my_Point.y();
	
	 //Update record points when moving
	my_Point  = event->globalPos();
	
	//Window moves here
	move(x() + dx, y() + dy); 
}
//Mouse release
void mouseReleaseEvent(QMouseEvent *event)
{
	//Record coordinates moved to
	int dx = event->globalX() - my_Point.x();
	int dy = event->globalY() - my_Point.y();
	//window moving
	move(x() + dx, y() + dy);
}

Close and narrow the window

Create button
this->minButton = new QPushButton(this);
this->closeButton= new QPushButton(this);

//Connection slot function
this->connect(minButton,SIGNAL(clicked()),this,SLOT(minButtonClick()));
this->connect(closeButton,SIGNAL(clicked()),this,SLOT(closeButtonClick()));
//The following button attribute settings (position, size, text, etc.) are not proposed here

//Button slot function definition
//Zoom out button
void YourClassName::minButtonClick()
{
	//contract the window
	this->showMinimized();
}

//close button
void YourClassName::closeButtonClick()
{
	//close window
	this->close();
}

Topics: Attribute