In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.