Most intuitive way to do this would be clicking on the widget surface and dragging. In order to achieve this, you need to follow some steps.
The method goes as follows: When the user presses inside the widget, set a boolean flag and store the position of the mouse and then when the button is released, set it to false. The next step is moving the widget. In the mouseMoveEvent, check if the flag is set. If it is set, take the new position of the mouse. Calculate the difference between new position and the stored one. Then, set the position of the window to original position + the calculated mouse movement. Then store the new mouse position.
The code required would be this:
/// Header contents:
class MyWidget : public QMainWindow
{
protected:
void mouseMoveEvent(QMouseEvent* event);
void mousePressEvent(QMouseEvent* event);
void mouseReleaseEvent(QMouseEvent* event);
private:
QPoint mLastMousePosition;
bool mMoving;
}
/// Source:
void MyWidget::mousePressEvent(QMouseEvent* event)
{
if(event->button() == Qt::LeftButton)
{
mMoving = true;
mLastMousePosition = event->pos();
}
}
void MyWidget::mouseMoveEvent(QMouseEvent* event)
{
if( event->buttons().testFlag(Qt::LeftButton) && mMoving)
{
this->move(this->pos() + (event->pos() - mLastMousePosition));
mLastMousePosition = event->pos();
}
}
void MyWidget::mouseReleaseEvent(QMouseEvent* event)
{
if(event->button() == Qt::LeftButton)
{
mMoving = false;
}
}