最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

QT 去掉标题栏和去掉标题栏后移动窗口

运维笔记admin25浏览0评论

转自:http://www.2cto/kf/201302/191602.html

在用QT编写界面时,去掉标题栏方法比较简单,就一行代码
this->setWindowFlags(Qt::FramelessWindowHint);
去掉以后又发现一个问题,就是不能移动窗口了,于是我就重写了三个鼠标事件,大致代码如下 .h文件的代码:
#include <QMouseEvent>
protected:
    void mousePressEvent(QMouseEvent *e);
    void mouseMoveEvent(QMouseEvent *e);
    void mouseReleaseEvent(QMouseEvent *e);
private:
    QPoint last;
.cpp文件的代码
//可以在构造函数中初始一下last变量用其成员函数setX,setY就是了
//接下来就是对三个鼠标事件的重写
void MainWindow::mousePressEvent(QMouseEvent *e)
{
    last = e->globalPos();
}
void MainWindow::mouseMoveEvent(QMouseEvent *e)
{
    int dx = e->globalX() - last.x();
    int dy = e->globalY() - last.y();
    last = e->globalPos();
    move(x()+dx, y()+dy);
}
void MainWindow::mouseReleaseEvent(QMouseEvent *e)
{
    int dx = e->globalX() - last.x();
    int dy = e->globalY() - last.y();
    move(x()+dx, y()+dy);
}
复制过去用的时候记得把类名改掉哦~ 这样就OK了,去掉窗口标题栏后还能拖动窗体

发布评论

评论列表(0)

  1. 暂无评论