新增通用控件移动类

master
飞扬青云 2019-09-28 23:30:33 +08:00 committed by Gitee
parent 0feb8a4cf0
commit 16f1e309ff
7 changed files with 243 additions and 0 deletions

View File

@ -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);
}

View File

@ -0,0 +1,26 @@
#ifndef FRMMOVEWIDGET_H
#define FRMMOVEWIDGET_H
#include <QWidget>
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

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>frmMoveWidget</class>
<widget class="QWidget" name="frmMoveWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>500</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
</widget>
<resources/>
<connections/>
</ui>

31
movewidget/main.cpp Normal file
View File

@ -0,0 +1,31 @@
#pragma execution_character_set("utf-8")
#include "frmmovewidget.h"
#include <QApplication>
#include <QTextCodec>
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();
}

52
movewidget/movewidget.cpp Normal file
View File

@ -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;
}

46
movewidget/movewidget.h Normal file
View File

@ -0,0 +1,46 @@
#ifndef MOVEWIDGET_H
#define MOVEWIDGET_H
/**
* :feiyangqingyun(QQ:517216493) 2019-9-28
* 1:widget
* 2:
* 3:widget
*/
#include <QWidget>
#ifdef quc
#if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
#include <QtDesigner/QDesignerExportWidget>
#else
#include <QtUiPlugin/QDesignerExportWidget>
#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

23
movewidget/movewidget.pro Normal file
View File

@ -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