Вот наваял от нечего делать пример, авось поможет. Перетягивать кнопку: Alt + LeftMouseButton, изменять размер: Alt + RightMouseButton:
main.cppC++ (Qt)
#include <QApplication>
#include <QMouseEvent>
#include "ui_Dialog.h"
class Resizer : public QObject
{
public:
Resizer( QWidget * widget ) :
widget_( widget )
{
isMousePressed_ = false;
widget_->installEventFilter( this );
}
bool eventFilter( QObject * o, QEvent * e )
{
switch ( e->type() )
{
case QEvent::MouseButtonPress:
{
QMouseEvent * me = static_cast<QMouseEvent*>( e );
if ( !(me->modifiers() & Qt::AltModifier) )
break;
isMousePressed_ = true;
pos_ = widget_->mapToGlobal( me->pos() );
}
return true;
case QEvent::MouseButtonRelease:
{
if ( !isMousePressed_ )
break;
isMousePressed_ = false;
}
return true;
case QEvent::MouseMove:
{
if ( !isMousePressed_ )
break;
QMouseEvent * me = static_cast<QMouseEvent*>( e );
const QPoint newPos = widget_->mapToGlobal( me->pos() );
const QPoint distance = newPos - pos_;
pos_ = newPos;
if ( me->buttons() & Qt::LeftButton )
widget_->move( widget_->pos() + distance );
else if ( me->buttons() & Qt::RightButton )
widget_->resize( widget_->size() + QSize( distance.x(), distance.y() ) );
}
return true;
}
return QObject::eventFilter( o, e );
}
private:
QWidget * widget_;
bool isMousePressed_;
QPoint pos_;
};
int main( int argc, char ** argv )
{
QApplication app( argc, argv );
QDialog dialog;
Ui::Dialog ui;
ui.setupUi( &dialog );
Resizer resizer( ui.button );
return dialog.exec();
}
Dialog.uiXML
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dialog</class>
<widget class="QDialog" name="Dialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QWidget" name="widget" native="true">
<widget class="QPushButton" name="button">
<property name="geometry">
<rect>
<x>120</x>
<y>100</y>
<width>141</width>
<height>41</height>
</rect>
</property>
<property name="text">
<string>Drag me with Alt+Mouse</string>
</property>
</widget>
</widget>
</item>
<item>
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>Dialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>Dialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>
CMakeLists.txtcmake_minimum_required( VERSION 2.6 )
find_package( Qt4 REQUIRED )
include( "${QT_USE_FILE}" )
include_directories( "${CMAKE_CURRENT_BINARY_DIR}" )
qt4_wrap_ui( Reqizer_UI_HEADERS "Dialog.ui" )
add_executable( Resizer ${Reqizer_UI_HEADERS} "main.cpp" )
target_link_libraries( Resizer ${QT_LIBRARIES} )