QT 自定义无边框窗体,支持边缘拖拽缩放。搬运自http://www.cnblogs.com/xufeiyang/p/3313104.html,稍改了些代码,供参考
代码片段和文件信息
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 1034026 2013-02-25 13:58 Sizableframeg_main.png
文件 5724 2016-04-20 15:37 Sizableframedialog.cpp
文件 809 2013-09-10 16:57 Sizableframedialog.h
文件 441 2013-09-10 13:53 Sizableframedialog.ui
文件 90 2013-09-10 13:54 Sizableframeimage.qrc
文件 179 2013-09-10 13:53 Sizableframemain.cpp
文件 413 2016-04-20 10:33 SizableframeSizableframe.pro
文件 34022 2016-04-20 16:49 SizableframeSizableframe.pro.user
文件 17738 2013-09-10 13:57 SizableframeSizableframe.pro.user.3.0-pre1
目录 0 2016-04-20 16:49 Sizableframe
----------- --------- ---------- ----- ----
1093442 10
#include “dialog.h“
#include “ui_dialog.h“
Dialog::Dialog(QWidget *parent) :
QDialog(parent)
ui(new Ui::Dialog)
{
ui->setupUi(this);
isLeftPressDown = false;
this->dir = NONE;
this->setMinimumHeight(100);
this->setMinimumWidth(150);
this->setWindowFlags(Qt::framelessWindowHint|Qt::WindowSystemMenuHint );
this->setMouseTracking(true);
this->setstyleSheet(“QDialog{background:url(:/bg_main.png)}“);
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::mouseReleaseEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton) {
isLeftPressDown = false;
//if(dir != NONE) {
this->releaseMouse();
this->setCursor(QCursor(Qt::ArrowCursor));
//}
}
}
void Dialog::mousePressEvent(QMouseEvent *event)
{
switch(event->button()) {
case Qt::LeftButton:
isLeftPressDown = true;
if(dir != NONE) {
this->mouseGrabber();
} else {
dragPosition = event->globalPos() - this->frameGeometry().topLeft();
}
break;
case Qt::RightButton:
this->close();
break;
default:
QDialog::mousePressEvent(event);
}
}
void Dialog::mouseMoveEvent(QMouseEvent *event)
{
QPoint gloPoint = event->globalPos();
QRect rect = this->rect();
QPoint tl = mapToGlobal(rect.topLeft());
QPoint rb = mapToGlobal(rect.bottomRight());
if(!isLeftPressDown) {
this->region(gloPoint);
} else {
if(dir != NONE) {
QRect rMove(tl rb);
switch(dir) {
case LEFT:
if(rb.x() - gloPoint.x() <= this->minimumWidth())
rMove.setX(tl.x());
else
rMove.setX(gloPoint.x());
break;
case RIGHT:
rMove.setWidth(gloPoint.x() - tl.x());
break;
case UP:
if(rb.y() - gloPoint.y() <= this->minimumHeight())
rMove.setY(tl.y());
else
rMove.setY(gloPoint.y());
break;
case DOWN:
rMove.setHeight(gloPoint.y() - tl.y());
break;
case LEFTTOP:
if(rb.x() - gloPoint.x() <= this->minimumWidth())
rMove.setX(tl.x());
else
rMove.setX(gloPoint.x());
if(rb.y() - gloPoint.y() <= this->minimumHeight())
rMove.setY(tl.y());
else
rMove.setY(gloPoint.y());
break;
case RIGHTTOP:
rMove.setWidth(gloPoint.x() - tl.x());
if(rb.y() - gloPoint.y() <= this->minimumHeight())
rMove.setY(tl.y());
else
rMove.setY(gloPoint.y());
break;
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 1034026 2013-02-25 13:58 Sizablefr
文件 5724 2016-04-20 15:37 Sizablefr
文件 809 2013-09-10 16:57 Sizablefr
文件 441 2013-09-10 13:53 Sizablefr
文件 90 2013-09-10 13:54 Sizablefr
文件 179 2013-09-10 13:53 Sizablefr
文件 413 2016-04-20 10:33 Sizablefr
文件 34022 2016-04-20 16:49 Sizablefr
文件 17738 2013-09-10 13:57 Sizablefr
目录 0 2016-04-20 16:49 Sizablefr
----------- --------- ---------- ----- ----
1093442 10
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。
评论列表(条)