您的位置:首页 > 编程语言 > Qt开发

Qt无边框、不规则窗口、窗口移动以及右键菜单

2017-08-18 11:55 435 查看
目的:

①不规则的窗口,无边框是必然的。

②窗口可以鼠标左键移动。

③鼠标右键点击,出现Close菜单,点击关闭程序。

步骤:

1、寻找“不规则窗口”的图片资源,随便百度一张就行了。*.png格式。我找的是这个。



2、新建Qt工程。
3、上代码。
说明:本程序不需要*.ui文件。
技术细节:
1.窗口设置
设置窗口形态/模态,Qt::FramelessWindowHint为无边框,Qt::WindowStaysOnTopHint为顶层窗口
设置窗口属性,Qt::WA_TranslucentBackground为背景透明。
加载QPixmap。
重置窗口大小,设置成对应QPixmap的尺寸。
this->setWindowFlags(Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);

this->setAttribute(Qt::WA_TranslucentBackground);

this->pix.load("./1.png");

this->resize(pix.size());


2.实现如下几个事件函数。
无边框窗口的鼠标移动需要实现mousePressEvent、mouseMoveEvent、mouseReleaseEvent事件,使用move()函数来实现移动。
不规则窗口需要实现paintEvent事件,画出QPixmap的图片资源作为窗口。
右键菜单需要实现contextMenuEvent事件。
My_Close()为响应右键菜单的槽函数。

void Widget::mousePressEvent(QMouseEvent *e)

{

this->currentMousePoint = e->globalPos();

this->changePos = e->globalPos()-this->pos();

}


void Widget::mouseMoveEvent(QMouseEvent *e)

{

if(e->buttons()==Qt::LeftButton)

{

this->move(e->globalPos()-this->changePos);

}

if(e->buttons()==Qt::RightButton)

{

this->currentMousePoint = e->globalPos();

}

}


void Widget::mouseReleaseEvent(QMouseEvent *e)

{


}


void Widget::My_Close()

{

this->close();

}


void Widget::paintEvent(QPaintEvent *)

{

QPainter painter(this);

painter.drawPixmap(0,0,this->pix);

}


void Widget::contextMenuEvent(QContextMenuEvent *)

{

QMenu menu(this);

menu.addAction("Close",this,SLOT(My_Close()));

menu.move(this->currentMousePoint);

menu.exec();

}


-----------------------------------------------------------------------------------------------------------------------------

以下是完整的源代码:
widget.h:

#ifndef WIDGET_H

#define WIDGET_H


#include <QWidget>

#include <QPixmap>

#include <QPoint>


namespace Ui {

class Widget;

}


class Widget : public QWidget

{

Q_OBJECT


public:

explicit Widget(QWidget *parent = 0);

~Widget();


protected:

void mousePressEvent(QMouseEvent *);

void mouseMoveEvent(QMouseEvent *);

void mouseReleaseEvent(QMouseEvent *);


private slots:

void paintEvent(QPaintEvent *);

void contextMenuEvent(QContextMenuEvent *);

void My_Close();


private:

Ui::Widget *ui;

QPixmap pix;

QPoint currentMousePoint;

QPoint changePos;

};


#endif // WIDGET_H



-----------------------------------------------------------------------------------------------------------------------------

widget.cpp

#include "widget.h"

#include "ui_widget.h"

#include <QPainter>

#include <QMenu>

#include <QMouseEvent>

#include <QDebug>


Widget::Widget(QWidget *parent) :

QWidget(parent),

ui(new Ui::Widget)

{

ui->setupUi(this);

this->setWindowFlags(Qt::FramelessWindowHint|Qt::WindowStaysOnTopHint);

this->setAttribute(Qt::WA_TranslucentBackground);

this->pix.load("./1.png");

this->resize(pix.size());

}


Widget::~Widget()

{

delete ui;

}


void Widget::mousePressEvent(QMouseEvent *e)

{

this->currentMousePoint = e->globalPos();

this->changePos = e->globalPos()-this->pos();

}


void Widget::mouseMoveEvent(QMouseEvent *e)

{

if(e->buttons()==Qt::LeftButton)

{

this->move(e->globalPos()-this->changePos);

}

if(e->buttons()==Qt::RightButton)

{

this->currentMousePoint = e->globalPos();

}

}


void Widget::mouseReleaseEvent(QMouseEvent *e)

{


}


void
bf2e
Widget::My_Close()

{

this->close();

}


void Widget::paintEvent(QPaintEvent *)

{

QPainter painter(this);

painter.drawPixmap(0,0,this->pix);

}


void Widget::contextMenuEvent(QContextMenuEvent *)

{

QMenu menu(this);

menu.addAction("Close",this,SLOT(My_Close()));

menu.move(this->currentMousePoint);

menu.exec();

}


-----------------------------------------------------------------------------------------------------------------------------

main.cpp

#include <QtGui/QApplication>

#include "widget.h"


int main(int argc, char *argv[])

{

QApplication a(argc, argv);

Widget w;

w.show();


return a.exec();

}


最终效果图:




-----------------------------------------------------------------------------------------------------------------------------

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息