在窗口上贴图
void IrrWid::paintEvent(QPaintEvent *)
{
QPainter p(this);
p.drawPixmap(rect(),QPixmap(":/new/prefix1/C:/Users/13764/Pictures/Saved Pictures/timgBOZIAIW6.jpg"));
}
-----------------------------------
去除窗口的边框:windowFlags函数是为了获取窗口原来的标记
//去除窗口边框
setWindowFlags(Qt::FramelessWindowHint|windowFlags());//windowFlags获取原来标记
--------------------------------------
把窗口背景设置为透明
//把窗口背景设置为透明
setAttribute(Qt::WA_TranslucentBackground);
--------------------------------------
处理鼠标点击的信息:如果点击右键就退出程序,如果点击左键就获取坐标差值
void IrrWid::mousePressEvent(QMouseEvent *e)
{
if(e->button()==Qt::RightButton)
{
close();
}
else if(e->button()==Qt::LeftButton)
{
//求坐标差值
//当前点击坐标-窗口左上角
p=e->globalPos()-this->frameGeometry().topLeft();
}
}
-------------------------------------
处理鼠标移动信息:如果鼠标长时间点击不放同时是鼠标左键,那么配合着鼠标点击的信息处理就可以做到拖着不规则窗口移动的功能。
减去相对坐标差是很有必要的!只有这样move函数移动的才是正确的左上角的坐标!!
void IrrWid::mouseMoveEvent(QMouseEvent *e)
{
if(e->buttons()&Qt::LeftButton)
{
move(e->globalPos()-p);
}
}
------------------------------------------
运行:
我使用鼠标左键长时间点击并拖动图片可以实现图片的移动,点击右键可以结束程序!!