From b40faeb3e01818eed2520cc807c5a0dd8e801fd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=A3=9E=E6=89=AC=E9=9D=92=E4=BA=91?= Date: Sat, 28 Sep 2019 22:21:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- movewidget/frmmovewidget.cpp | 46 +++++++++++++++++++++++++++++++ movewidget/frmmovewidget.h | 26 ++++++++++++++++++ movewidget/frmmovewidget.ui | 19 +++++++++++++ movewidget/main.cpp | 31 +++++++++++++++++++++ movewidget/movewidget.cpp | 52 ++++++++++++++++++++++++++++++++++++ movewidget/movewidget.h | 46 +++++++++++++++++++++++++++++++ movewidget/movewidget.pro | 23 ++++++++++++++++ 7 files changed, 243 insertions(+) create mode 100644 movewidget/frmmovewidget.cpp create mode 100644 movewidget/frmmovewidget.h create mode 100644 movewidget/frmmovewidget.ui create mode 100644 movewidget/main.cpp create mode 100644 movewidget/movewidget.cpp create mode 100644 movewidget/movewidget.h create mode 100644 movewidget/movewidget.pro diff --git a/movewidget/frmmovewidget.cpp b/movewidget/frmmovewidget.cpp new file mode 100644 index 0000000..0d88099 --- /dev/null +++ b/movewidget/frmmovewidget.cpp @@ -0,0 +1,46 @@ +#pragma execution_character_set("utf-8") + +#include "frmmovewidget.h" +#include "ui_frmmovewidget.h" +#include "qpushbutton.h" +#include "qprogressbar.h" +#include "movewidget.h" + +frmMoveWidget::frmMoveWidget(QWidget *parent) : QWidget(parent), ui(new Ui::frmMoveWidget) +{ + ui->setupUi(this); + this->initForm(); +} + +frmMoveWidget::~frmMoveWidget() +{ + delete ui; +} + +void frmMoveWidget::initForm() +{ + QPushButton *btn1 = new QPushButton(this); + btn1->setGeometry(10, 10, 250, 25); + btn1->setText("按住我拖动(仅限左键拖动)"); + MoveWidget *moveWidget1 = new MoveWidget(this); + moveWidget1->setWidget(btn1); + + QPushButton *btn2 = new QPushButton(this); + btn2->setGeometry(10, 40, 250, 25); + btn2->setText("按住我拖动(支持右键拖动)"); + MoveWidget *moveWidget2 = new MoveWidget(this); + moveWidget2->setWidget(btn2); + moveWidget2->setLeftButton(false); + + QProgressBar *bar = new QProgressBar(this); + bar->setGeometry(10, 70, 250, 25); + bar->setRange(0, 0); + bar->setTextVisible(false); + MoveWidget *moveWidget3 = new MoveWidget(this); + moveWidget3->setWidget(bar); +} + + + + + diff --git a/movewidget/frmmovewidget.h b/movewidget/frmmovewidget.h new file mode 100644 index 0000000..67bce7d --- /dev/null +++ b/movewidget/frmmovewidget.h @@ -0,0 +1,26 @@ +#ifndef FRMMOVEWIDGET_H +#define FRMMOVEWIDGET_H + +#include + +namespace Ui +{ + class frmMoveWidget; +} + +class frmMoveWidget : public QWidget +{ + Q_OBJECT + +public: + explicit frmMoveWidget(QWidget *parent = 0); + ~frmMoveWidget(); + +private: + Ui::frmMoveWidget *ui; + +private slots: + void initForm(); +}; + +#endif // FRMMOVEWIDGET_H diff --git a/movewidget/frmmovewidget.ui b/movewidget/frmmovewidget.ui new file mode 100644 index 0000000..86ddc45 --- /dev/null +++ b/movewidget/frmmovewidget.ui @@ -0,0 +1,19 @@ + + + frmMoveWidget + + + + 0 + 0 + 500 + 300 + + + + Form + + + + + diff --git a/movewidget/main.cpp b/movewidget/main.cpp new file mode 100644 index 0000000..f094e03 --- /dev/null +++ b/movewidget/main.cpp @@ -0,0 +1,31 @@ +#pragma execution_character_set("utf-8") + +#include "frmmovewidget.h" +#include +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + a.setFont(QFont("Microsoft Yahei", 9)); + +#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0)) +#if _MSC_VER + QTextCodec *codec = QTextCodec::codecForName("gbk"); +#else + QTextCodec *codec = QTextCodec::codecForName("utf-8"); +#endif + QTextCodec::setCodecForLocale(codec); + QTextCodec::setCodecForCStrings(codec); + QTextCodec::setCodecForTr(codec); +#else + QTextCodec *codec = QTextCodec::codecForName("utf-8"); + QTextCodec::setCodecForLocale(codec); +#endif + + frmMoveWidget w; + w.setWindowTitle("通用移动类"); + w.show(); + + return a.exec(); +} diff --git a/movewidget/movewidget.cpp b/movewidget/movewidget.cpp new file mode 100644 index 0000000..70d2181 --- /dev/null +++ b/movewidget/movewidget.cpp @@ -0,0 +1,52 @@ +#include "movewidget.h" +#include "qevent.h" +#include "qdebug.h" + +MoveWidget::MoveWidget(QObject *parent) : QObject(parent) +{ + lastPoint = QPoint(0, 0); + pressed = false; + leftButton = 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 (leftButton && mouseEvent->button() != Qt::LeftButton) { + return false; + } + + //判断控件的区域是否包含了当前鼠标的坐标 + if (widget->rect().contains(mouseEvent->pos())) { + lastPoint = mouseEvent->pos(); + pressed = true; + } + } else if (mouseEvent->type() == QEvent::MouseMove && pressed) { + //计算坐标偏移值,调用move函数移动过去 + int offsetX = mouseEvent->pos().x() - lastPoint.x(); + int offsetY = mouseEvent->pos().y() - lastPoint.y(); + widget->move(widget->x() + offsetX, widget->y() + offsetY); + } else if (mouseEvent->type() == QEvent::MouseButtonRelease && pressed) { + pressed = false; + } + } + + return QObject::eventFilter(watched, event); +} + +void MoveWidget::setWidget(QWidget *widget) +{ + if (this->widget == 0) { + this->widget = widget; + this->widget->installEventFilter(this); + } +} + +void MoveWidget::setLeftButton(bool leftButton) +{ + this->leftButton = leftButton; +} diff --git a/movewidget/movewidget.h b/movewidget/movewidget.h new file mode 100644 index 0000000..a8e23cb --- /dev/null +++ b/movewidget/movewidget.h @@ -0,0 +1,46 @@ +#ifndef MOVEWIDGET_H +#define MOVEWIDGET_H + +/** + * 通用控件移动类 作者:feiyangqingyun(QQ:517216493) 2019-9-28 + * 1:可以指定需要移动的widget + * 2:可设置是否限定鼠标左键拖动 + * 3:支持任意widget控件 + */ + +#include + +#ifdef quc +#if (QT_VERSION < QT_VERSION_CHECK(5,7,0)) +#include +#else +#include +#endif + +class QDESIGNER_WIDGET_EXPORT MoveWidget : public QObject +#else +class MoveWidget : public QObject +#endif + +{ + Q_OBJECT +public: + explicit MoveWidget(QObject *parent = 0); + +protected: + bool eventFilter(QObject *watched, QEvent *event); + +private: + QPoint lastPoint; //最后按下的坐标 + bool pressed; //鼠标是否按下 + bool leftButton; //限定鼠标左键 + QWidget *widget; //移动的控件 + +public Q_SLOTS: + //设置是否限定鼠标左键 + void setLeftButton(bool leftButton); + //设置要移动的控件 + void setWidget(QWidget *widget); +}; + +#endif // MOVEWIDGET_H diff --git a/movewidget/movewidget.pro b/movewidget/movewidget.pro new file mode 100644 index 0000000..31ea681 --- /dev/null +++ b/movewidget/movewidget.pro @@ -0,0 +1,23 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2019-09-28T15:04:18 +# +#------------------------------------------------- + +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = lightbutton +TEMPLATE = app +DESTDIR = $$PWD/../bin +CONFIG += warn_off + +SOURCES += main.cpp +SOURCES += frmmovewidget.cpp +SOURCES += movewidget.cpp + +HEADERS += frmmovewidget.h +HEADERS += movewidget.h + +FORMS += frmmovewidget.ui