From b154aad0d1a9081fa77a4cf027f62e7c529a06a3 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:26:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=20movewidg?= =?UTF-8?q?et?= 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 deletions(-) delete mode 100644 movewidget/frmmovewidget.cpp delete mode 100644 movewidget/frmmovewidget.h delete mode 100644 movewidget/frmmovewidget.ui delete mode 100644 movewidget/main.cpp delete mode 100644 movewidget/movewidget.cpp delete mode 100644 movewidget/movewidget.h delete mode 100644 movewidget/movewidget.pro diff --git a/movewidget/frmmovewidget.cpp b/movewidget/frmmovewidget.cpp deleted file mode 100644 index 0d88099..0000000 --- a/movewidget/frmmovewidget.cpp +++ /dev/null @@ -1,46 +0,0 @@ -#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 deleted file mode 100644 index 67bce7d..0000000 --- a/movewidget/frmmovewidget.h +++ /dev/null @@ -1,26 +0,0 @@ -#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 deleted file mode 100644 index 86ddc45..0000000 --- a/movewidget/frmmovewidget.ui +++ /dev/null @@ -1,19 +0,0 @@ - - - frmMoveWidget - - - - 0 - 0 - 500 - 300 - - - - Form - - - - - diff --git a/movewidget/main.cpp b/movewidget/main.cpp deleted file mode 100644 index f094e03..0000000 --- a/movewidget/main.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#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 deleted file mode 100644 index 70d2181..0000000 --- a/movewidget/movewidget.cpp +++ /dev/null @@ -1,52 +0,0 @@ -#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 deleted file mode 100644 index a8e23cb..0000000 --- a/movewidget/movewidget.h +++ /dev/null @@ -1,46 +0,0 @@ -#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 deleted file mode 100644 index 31ea681..0000000 --- a/movewidget/movewidget.pro +++ /dev/null @@ -1,23 +0,0 @@ -#------------------------------------------------- -# -# 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