QT——入门之窗口设置

头文件

#include <QIcon>	//图片加载
#include <QPalette>    	//调色板	
#include <QBrush>      	//笔刷
#include <QBitmap>     	//用于去除图片的空白部分

设置窗口的图标和标题

//设置成最大窗口,不可改变窗口大小
this->setFixedSize(481,579);      

//设置可变大小的窗口	 	
//  this->resize(481, 579);				

//设置窗口名称
this->setWindowTitle("我的窗口");

 //设置窗口的图标
this->setWindowIcon(QIcon("image/gameUi/AF2.png"));     

设置图片为窗口背景

QPixmap pixmap_Background("image/gameUi/01.png");	//图片的地址

//建立调色板
QPalette palette;   		

//用笔刷来设置背景,目标为背景,使用图片来填充
palette.setBrush(QPalette::Background,QBrush(QPixmap(pixmap_Background)));  

//窗口应用调色板
this->setPalette(palette);     

//可以将图片中透明部分显示为透明的 需要头文件<QBitmap>或者<QtGui/QtGui>,如有使用请看设置窗口移动
//  this->setMask(pixmap_Background.mask());          

//设置窗体自动填充背景
this->setAutoFillBackground(true);                

设置窗口移动

this->setMask(pixmap_Background.mask());
实现效果:窗口的轮廓将和你图片轮廓一致
遇到问题:窗口的关闭、缩小按钮会被屏蔽,窗口移动不了
解决方法:添加按钮来控制窗口关闭、缩小,重载鼠标事件来控制窗口移动

//建立一个QPoint 来接收鼠标点下来的点
QPoint my_Point;

 //重载鼠标函数——按下鼠标事件
void mousePressEvent(QMouseEvent *event)
{
	//读取坐鼠标点击坐标点
	my_Point = event->globalPos();
}   

///重载鼠标函数——鼠标移动,可以实现你按着鼠标移动到哪,窗口就移动到哪里
void mouseMoveEvent(QMouseEvent *event)
{
	//把移动的点记录下来
	int dx = event->globalX() - my_Point.x();
	int dy = event->globalY() - my_Point.y();
	
	 //移动就要更新记录点
	my_Point  = event->globalPos();
	
	//窗口移动到此处
	move(x() + dx, y() + dy); 
}
//鼠标释放
void mouseReleaseEvent(QMouseEvent *event)
{
	//记录移动到的坐标
	int dx = event->globalX() - my_Point.x();
	int dy = event->globalY() - my_Point.y();
	//窗口移动
	move(x() + dx, y() + dy);
}

关闭、缩小窗口

建立按钮
this->minButton = new QPushButton(this);
this->closeButton= new QPushButton(this);

//连接槽函数
this->connect(minButton,SIGNAL(clicked()),this,SLOT(minButtonClick()));
this->connect(closeButton,SIGNAL(clicked()),this,SLOT(closeButtonClick()));
//后续的一些对按钮属性设置(位置、大小、文字等)这里就不一一提出了

//按钮的槽函数定义
//缩小按钮
void YourClassName::minButtonClick()
{
	//缩小窗口
	this->showMinimized();
}

//关闭按钮
void YourClassName::closeButtonClick()
{
	//关闭窗口
	this->close();
}
原创文章 3 获赞 1 访问量 32

猜你喜欢

转载自blog.csdn.net/l1206715877/article/details/106171999