1
0
mirror of https://gitee.com/drabel/LibQQt.git synced 2025-01-04 10:18:44 +08:00

增加子窗口移动器。

This commit is contained in:
tianduanrui 2019-09-22 19:22:29 +08:00
parent a9d7677db3
commit 13c6a06c9b
6 changed files with 179 additions and 4 deletions

View File

@ -23,6 +23,9 @@
* move的时候使
* QWidget及其子类QMainWindow
* windowsmacOSLinuxe-linux等操作系统
*
*
* WindowsmacOSLinux等系统下表现一致
*/
class QQtBodyMoverPrivate;
class QQTSHARED_EXPORT QQtBodyMover : public QObject

View File

@ -16,7 +16,7 @@ public:
QMargins& margins();
const QMargins& margins() const;
public:
protected:
virtual void mousePressEvent ( QMouseEvent* event, QWidget* target = 0 );
virtual void mouseReleaseEvent ( QMouseEvent* event, QWidget* target = 0 );
virtual void mouseMoveEvent ( QMouseEvent* event, QWidget* target = 0 );

View File

@ -28,7 +28,7 @@ public:
QMargins& margins();
const QMargins& margins() const;
public:
protected:
virtual void mousePressEvent ( QMouseEvent* event, QWidget* target = 0 );
virtual void mouseReleaseEvent ( QMouseEvent* event, QWidget* target = 0 );
virtual void mouseMoveEvent ( QMouseEvent* event, QWidget* target = 0 );

View File

@ -0,0 +1,119 @@
#include <qqtchildbodymover.h>
QQtChildBodyMover::QQtChildBodyMover ( QObject* parent ) : QObject ( parent )
{
bMousePressed = false;
m_margins = QMargins ( 10, 10, 10, 10 );
}
QQtChildBodyMover::~QQtChildBodyMover() {}
QMargins& QQtChildBodyMover::margins()
{
return m_margins;
}
void QQtChildBodyMover::mousePressEvent ( QMouseEvent* event, QWidget* target )
{
Q_ASSERT ( target );
//以下代码用来过滤边缘的margin。
#if 1
//maptoGlobal(rect()) 与 globalPos 对比
QRect rectMustIn = QRect ( target->mapToGlobal ( target->rect().topLeft() ), target->mapToGlobal ( target->rect().bottomRight() ) );
#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
QRect rectMustNotIn = rectMustIn.adjusted ( m_margins.left(), m_margins.top(), m_margins.right(), m_margins.bottom() );
#else
QRect rectMustNotIn = rectMustIn.marginsRemoved ( m_margins );
#endif
QPoint cursorPos = event->globalPos();
//经过测试这种方法在子窗口、root窗口得到的数据完全正常。
//pline() << target->geometry() << rectMustIn
// << rectMustIn.contains ( event->globalPos() ) << rectMustNotIn.contains ( event->globalPos() );
#endif
if ( target->isMaximized() ||
!target->isActiveWindow() ||
!rectMustIn.contains ( cursorPos ) ||
!rectMustNotIn.contains ( cursorPos ) )
{
event->ignore();
return;
}
if ( event->button() == Qt::LeftButton )
{
bMousePressed = true;
pressedPoint = event->globalPos();
}
event->accept();
}
void QQtChildBodyMover::mouseReleaseEvent ( QMouseEvent* event, QWidget* target )
{
bMousePressed = false;
event->accept();
}
void QQtChildBodyMover::mouseMoveEvent ( QMouseEvent* event, QWidget* target )
{
Q_ASSERT ( target );
QWidget* win = target->window();
if ( bMousePressed && !win->isMaximized() )
{
QPoint p1 = event->globalPos();
QPoint pxy = p1 - pressedPoint;
pressedPoint = p1;
QPoint p0 = target->geometry().topLeft();
QPoint p2 = p0 + pxy;
target->move ( p2 );
}
event->accept();
}
bool QQtChildBodyMover::eventFilter ( QObject* watched, QEvent* event )
{
//过滤掉不是QWidget
if ( !watched->inherits ( "QWidget" ) )
return QObject::eventFilter ( watched, event );
//修复 paint bug
/*fix the parent handled bug terminally*/
if ( event->type() == QEvent::Paint )
return QObject::eventFilter ( watched, event );
//修复鼠标穿透。
bool atti = ( qobject_cast<QWidget*> ( watched ) )->testAttribute ( Qt::WA_TransparentForMouseEvents );
if ( atti )
return QObject::eventFilter ( watched, event );
switch ( event->type() )
{
case QEvent::MouseButtonPress:
{
QMouseEvent* e = ( QMouseEvent* ) event;
mousePressEvent ( e, qobject_cast<QWidget*> ( watched ) );
return false;
}
case QEvent::MouseButtonRelease:
{
QMouseEvent* e = ( QMouseEvent* ) event;
mouseReleaseEvent ( e, qobject_cast<QWidget*> ( watched ) );
return false;
}
case QEvent::MouseMove:
{
QMouseEvent* e = ( QMouseEvent* ) event;
mouseMoveEvent ( e, qobject_cast<QWidget*> ( watched ) );
return false;
}
default:
break;
}
return QObject::eventFilter ( watched, event );
}

View File

@ -0,0 +1,45 @@
#ifndef QQTCHILDBODYMOVER_H
#define QQTCHILDBODYMOVER_H
#include <QObject>
#include <QMargins>
#include <QMouseEvent>
#include <QWidget>
#include <qqt-local.h>
/**
* @brief The QQtChildBodyMover class
*
*
* 使
*
* WindowsmacOSLinux等系统下表现一致
*/
class QQTSHARED_EXPORT QQtChildBodyMover : public QObject
{
Q_OBJECT
public:
QQtChildBodyMover ( QObject* parent = 0 );
virtual ~QQtChildBodyMover();
QMargins& margins();
protected:
virtual void mousePressEvent ( QMouseEvent* event, QWidget* target = 0 );
virtual void mouseReleaseEvent ( QMouseEvent* event, QWidget* target = 0 );
virtual void mouseMoveEvent ( QMouseEvent* event, QWidget* target = 0 );
// QObject interface
public:
virtual bool eventFilter ( QObject* watched, QEvent* event ) override;
private:
bool bMousePressed;
QPoint pressedPoint;
QMargins m_margins;
};
#endif // QQTCHILDBODYMOVER_H

View File

@ -343,7 +343,7 @@ contains (DEFINES, __NETWORKSUPPORT__) {
contains (DEFINES, __EXQUISITE__) {
#exquisite
#窗体移动
#窗体整体移动 对父、子窗体均可使用,跨平台特征好
HEADERS += \
$$PWD/exquisite/qqtbodymover.h \
$$PWD/exquisite/qqtbodymover_p.h
@ -351,7 +351,7 @@ contains (DEFINES, __EXQUISITE__) {
$$PWD/exquisite/qqtbodymover.cpp \
$$PWD/exquisite/qqtbodymover_p.cpp
#窗体大小。一般用于无边框窗体
#窗体大小。一般用于无边框窗体。父、子窗体均可使用
HEADERS += \
$$PWD/exquisite/qqtbodyresizer.h \
$$PWD/exquisite/qqtbodyresizer_p.h
@ -359,6 +359,14 @@ contains (DEFINES, __EXQUISITE__) {
$$PWD/exquisite/qqtbodyresizer.cpp \
$$PWD/exquisite/qqtbodyresizer_p.cpp
#子窗体在父窗体里移动 只应用于子窗体,不能应用于主窗体
add_file($$PWD/exquisite/qqtchildbodymover.h)
add_file($$PWD/exquisite/qqtchildbodymover.cpp)
HEADERS += \
$$PWD/exquisite/qqtchildbodymover.h
SOURCES += \
$$PWD/exquisite/qqtchildbodymover.cpp
#不规则形状的控件
contains (DEFINES, __IRREGULARWIDGETS__) {
SOURCES += \