If your window is a frameless windows(via Qt::FramelessWindowHint) you can easily manage that in "real-time".
Following the "Analog-Clock" example, you can move the window by sub-classing both mousePressEvent() and mouseMoveEvent().
Here is a sample app that does just that:
Widget.cpp:
ui(new Ui::Widget)
{
ui->setupUi(this);
setWindowFlags(Qt::FramelessWindowHint);
filter = new EventFilter(this);
ui->widget->installEventFilter(filter);
move(300,0);
}
{
if(filter->canMove)
{
if (event->buttons() & Qt::LeftButton)
{
if(event->globalY() < 100){
qDebug() << "click move" << event->globalY();
move(event->globalPos() - dragPosition);
}else{
move(event->globalX() - dragPosition.x(),100);
}
event->accept();
}
}
}
{
qDebug() << "click away" << filter->canMove;
if (event->button() == Qt::LeftButton) {
dragPosition = event->globalPos() - frameGeometry().topLeft();
event->accept();
}
}
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
setWindowFlags(Qt::FramelessWindowHint);
filter = new EventFilter(this);
ui->widget->installEventFilter(filter);
move(300,0);
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
if(filter->canMove)
{
if (event->buttons() & Qt::LeftButton)
{
if(event->globalY() < 100){
qDebug() << "click move" << event->globalY();
move(event->globalPos() - dragPosition);
}else{
move(event->globalX() - dragPosition.x(),100);
}
event->accept();
}
}
}
void Widget::mousePressEvent(QMouseEvent *event)
{
qDebug() << "click away" << filter->canMove;
if (event->button() == Qt::LeftButton) {
dragPosition = event->globalPos() - frameGeometry().topLeft();
event->accept();
}
}
To copy to clipboard, switch view to plain text mode
Widget.h:
#include "eventFilter.h"
namespace Ui {
class Widget;
}
Q_OBJECT
public:
~Widget();
protected:
private:
Ui::Widget *ui;
EventFilter *filter;
};
#include "eventFilter.h"
namespace Ui {
class Widget;
}
class Widget : public QWidget {
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
protected:
void changeEvent(QEvent *e);
void mouseMoveEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
private:
Ui::Widget *ui;
QPoint dragPosition;
EventFilter *filter;
};
To copy to clipboard, switch view to plain text mode
eventFilter.h:
{
Q_OBJECT
public:
//
canMove = false;
}
bool canMove;
private:
protected:
{
if (event
->type
() == QEvent::MouseButtonPress ){ canMove = true;
}
else if(event
->type
() ==QEvent::MouseMove){ canMove = true;
}else{
canMove = false;
}
return QObject::eventFilter(obj, event
);
}
};
class EventFilter : public QWidget
{
Q_OBJECT
public:
EventFilter(QWidget *parent){
//
canMove = false;
}
bool canMove;
private:
QPoint dragPosition;
protected:
bool eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::MouseButtonPress ){
canMove = true;
}
else if(event->type() ==QEvent::MouseMove){
canMove = true;
}else{
canMove = false;
}
return QObject::eventFilter(obj, event);
}
};
To copy to clipboard, switch view to plain text mode
Keep in mind that if the user somehow moves your window(Windows can do that sometimes when it crashes or goes slow), you won't be able to move it back to the border until you restart your app. But if you check on moveEvent(), you can send it back if that ever happens...
Hope you find it useful.
Bookmarks