C++ (Qt)class AddAttachable: public QObject{ Q_OBJECT public: AddAttachable( QObject * parent = 0 ) : QObject( parent ), window( 0 ), timerId( -1 ), d_distance( 10 ), activate( true ) { } void setDistance( int d ) { d_distance = d; } int distance() { return d_distance; } void installTo( QWidget * widget ) { if ( window ) remove(); window = widget; window->installEventFilter( this ); } void remove() { window->removeEventFilter( this ); window = 0; } private: QWidget * window; int timerId; int d_distance; bool activate; public slots: void setActivate( bool act ) { activate = act; } bool isActivate() { return activate; } protected: void timerEvent( QTimerEvent * event ) { if ( !window || !activate ) // паранойя return; const QRect & availableGeometry = QDesktopWidget().availableGeometry(); int d_x = availableGeometry.x(); int d_y = availableGeometry.y(); int d_width = availableGeometry.width(); int d_height = availableGeometry.height(); int x = window->x(); int y = window->y(); int width = window->width(); int height = window->height(); if ( x >= d_x && x <= d_distance ) window->move( d_x, y ); if ( y >= d_y && y <= d_distance ) window->move( x, d_y ); if ( x + width >= d_width - d_distance && x + width <= d_width ) window->move( d_width - width, y ); if ( y + height >= d_height - d_distance && y + height <= d_height ) window->move( x, d_height - height ); } bool eventFilter( QObject * object, QEvent * event ) { if ( object == window && activate ) { if ( event->type() == QEvent::MouseButtonPress ) timerId = startTimer( 333 ); else if ( event->type() == QEvent::MouseButtonRelease ) killTimer( timerId ); } return QObject::eventFilter( object, event ); }}; class MovableWindow: public QWidget{public: MovableWindow( QWidget * parent = 0 ) : QWidget( parent ) { setWindowFlags( Qt::Window | Qt::FramelessWindowHint ); QHBoxLayout * layout = new QHBoxLayout(); layout->setContentsMargins( 0, 0, 0, 0 ); layout->addWidget( new QSizeGrip( this ), 0, Qt::AlignLeft | Qt::AlignTop ); layout->addWidget( new QSizeGrip( this ), 0, Qt::AlignRight | Qt::AlignBottom ); setLayout( layout ); setMinimumSize( 50, 50 ); resize( 150, 150 ); AddAttachable * addAttachable = new AddAttachable( this ); addAttachable->installTo( this ); } private: bool isMousePress; QPoint oldPosition; protected: void mousePressEvent( QMouseEvent * event ) { if ( event->button() == Qt::LeftButton ) { oldPosition = event->pos(); isMousePress = true; } QWidget::mousePressEvent( event ); } void mouseReleaseEvent( QMouseEvent * event ) { isMousePress = false; QWidget::mouseReleaseEvent( event ); } void mouseMoveEvent( QMouseEvent * event ) { if ( isMousePress ) { QPoint delta = event->pos() - oldPosition; move( pos() + delta ); } QWidget::mouseMoveEvent( event ); } void paintEvent( QPaintEvent * ) { QPainter painter( this ); painter.setPen( QPen( Qt::black, 2.0 ) ); painter.drawRect( rect() ); }};
C++ (Qt)bool MyWidget::event( QEvent * e ){ static bool inDrag = false; qDebug() << e->type(); switch ((int) e->type()) { case QEvent::NonClientAreaMouseButtonPress: inDrag = true; break; case QEvent::NonClientAreaMouseButtonRelease: if (inDrag) qApp->postEvent(this, new QEvent(QEvent::User)); inDrag = false; break; case QEvent::Move: if (!inDrag) qApp->postEvent(this, new QEvent(QEvent::User)); break; case QEvent::User: if (pos().x() < 50) move(QPoint(50, pos().y())); break; } return false;}
C++ (Qt) void timerEvent( QTimerEvent * event ) { if ( !window && !activate ) // паранойя return;