How to realize the Mobility of General Control by Qt
This article is about how Qt implements universal control movement. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.
I. Foreword
In the process of doing some projects, there is an application scenario that requires dragging the device in a container, freely dragging it to the appropriate position, and then saving the coordinate position information of the corresponding device. After the software is started, the configured coordinate position information will be automatically loaded, and each device will be moved to the corresponding position. It is best to have a 3D aerial view of the background image, or to have a 3D real scene, which is very dazzling. This is the reason for this control, there is also a scene such as configuration software, free drag design custom controls and pictures, etc., also need to drag around in the container, if there is a general control mobile class, directly new out into the widget that needs to be moved, so it is much more convenient, do not need each control or form itself to achieve this general repetitive function.
II. Code idea #include "movewidget.h"#include "qevent.h"#include "qdebug.h"MoveWidget::MoveWidget(QObject *parent) : QObject(parent){ lastPoint = QPoint(0, 0); pressed = false; leftButton = true; inControl = true; widget = 0;}bool MoveWidget::eventFilter(QObject *watched, QEvent *event){ if (widget != 0 && watched == widget) { QMouseEvent *mouseEvent = (QMouseEvent *)event; if (mouseEvent->type() == QEvent::MouseButtonPress) { //If you can only drag the left mouse button, determine whether the current mouse button is the left mouse button if (leftButton && mouseEvent->button() != Qt::LeftButton) { return false; } //Determine whether the control area contains the coordinates of the current mouse if (widget->rect().contains(mouseEvent->pos())) { lastPoint = mouseEvent->pos(); pressed = true; } } else if (mouseEvent->type() == QEvent::MouseMove && pressed) { //calculate the coordinate offset value, call the move function to move past int offsetX = mouseEvent->pos().x() - lastPoint.x(); int offsetY = mouseEvent->pos().y() - lastPoint.y(); int x = widget->x() + offsetX; int y = widget->y() + offsetY; if (inControl) { //You can adjust the scope of the container by yourself. By default, 20 pixels are reserved in it. int offset = 20; bool xyOut = (x + widget->width()
< offset || y + widget->height()
< offset); bool whOut = false; QWidget *w = (QWidget *)widget->parent(); if (w != 0) { whOut = (w->width() - x
< offset || w->height() - y
< offset); } if (xyOut || whOut) { return false; } } widget->move(x, y); } else if (mouseEvent->type() == QEvent::MouseButtonRelease && pressed) { pressed = false; } } return QObject::eventFilter(watched, event);}void MoveWidget::setLeftButton(bool leftButton){ this->leftButton = leftButton;}void MoveWidget::setInControl(bool inControl){ this->inControl = inControl;}void MoveWidget::setWidget(QWidget *widget){ if (this->widget == 0) { this->widget = widget; this->widget->installEventFilter(this); }} III. Effect Drawing
Thank you for reading! About "Qt how to achieve universal control mobile" this article is shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!