当前位置:网站首页>QT uses drag and drop picture to control and mouse to move picture
QT uses drag and drop picture to control and mouse to move picture
2022-04-23 03:20:00 【Happinessคิดถึง】
**
Update time :2021-04-15
**
New knowledge that I haven't done before ,qt Provided the interface , In fact, it is just to read the local file path .
Reference resources :
QT-Qpixmap Achieve picture mouse zoom , Mouse drag example
The effect is given below :
Here's the code :
The header file :
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QUrl>
#include <QList>
#include <QMimeData>
#include <QPainter>
#include <QDebug>
#include <QImage>
#include <QRect>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
// Mouse or keyboard operation
enum Type {
None = 0,
Amplification ,
Shrink,
Lift,
Right,
Up,
Down,
Move
};
protected:
// Drag and drop window
bool eventFilter(QObject *watched, QEvent *event);
void paintEvent(QPaintEvent *e);
bool event(QEvent * event);
private slots:
void on_pushButton_clicked();
private:
Ui::Widget *ui;
// Picture length and width
int picHeight,picWidth;
QImage pic; // picture
QRect Paint; // Painting area
QPoint Alloffset; // Total offset
QPoint offset; // One time image offset value
int action;
};
#endif // WIDGET_H
Implementation file :
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
// Install event filter
ui->widget->installEventFilter(this);
ui->widget->setAcceptDrops(true);
this->setMouseTracking(true);
Alloffset.setX(0);
Alloffset.setY(0);
action = Widget::None;
// Set the painting area -*- The coordinates of the upper left corner and the length and width of the label
// QPoint labelPos=ui->label->mapFromGlobal(QCursor::pos());// Global coordinates
Paint.setRect(9,9,388,582);
}
Widget::~Widget()
{
delete ui;
}
bool Widget::eventFilter(QObject *watched, QEvent *event)
{
if (watched == ui->widget)
{
if (event->type() == QEvent::DragEnter)
{
// [[2]]: When dragging and dropping, the mouse enters widget when , widget Accept drag and drop actions
QDragEnterEvent *dee = dynamic_cast<QDragEnterEvent *>(event);
dee->acceptProposedAction();
return true;
} else if (event->type() == QEvent::Drop)
{
// [[3]]: When the release operation occurs , Get drag and drop data
QDropEvent *de = dynamic_cast<QDropEvent *>(event);
QList<QUrl> urls = de->mimeData()->urls();
if (urls.isEmpty())
{
return true;
}
QString path = urls.first().toLocalFile();
// [[4]]: stay widget Show drag and drop pictures on
QImage image(path); // QImage Yes I/O Optimized , QPixmap Optimize the display .
pic=image;
if (!image.isNull())
{
// image = image.scaled(ui->label->size(),
// Qt::KeepAspectRatio,
// Qt::SmoothTransformation);
picHeight = image.height();
picWidth =image.width();
// qDebug()<<"pic wh:"<<picWidth<<picHeight;
this->update();
// ui->textEdit->append("pic h:"+QString::number(picHeight)+"w:"+QString::number(picWidth));
}
return true;
}
}
return QWidget::eventFilter(watched, event);
}
void Widget::paintEvent(QPaintEvent *e)
{
QPainter painter(this);
if(action==Widget::Move) // Move
{
int offsetx=Alloffset.x()+offset.x();
Alloffset.setX(offsetx);
int offsety=Alloffset.y()+offset.y();
Alloffset.setY(offsety);
qDebug()<<"alloffset:"<<offsetx<<offsety;
action=Widget::None;
}
// Limit X Offset value
if(abs(Alloffset.x())>=(Paint.width()/2 + picWidth/2 - ui->widget->x()))
{
if(Alloffset.x()>0)
Alloffset.setX(Paint.width()/2 + picWidth/2 - ui->widget->x());
else
Alloffset.setX(-Paint.width()/2 + -picWidth/2 + ui->widget->x());
}
// Limit Y Offset value
if(abs(Alloffset.y())>=(Paint.height()/2 + picHeight/2 - ui->widget->y()))
{
if(Alloffset.y()>0)
Alloffset.setY(Paint.height()/2 + picHeight/2 -ui->widget->y());
else
Alloffset.setY(-Paint.height()/2 + -picHeight/2 +ui->widget->y());
}
int x = Paint.width()/2 + Alloffset.x() -picWidth/2;
if(x<0)
x=0;
int y = Paint.height()/2 + Alloffset.y() -picHeight/2;
if(y<0)
y=0;
qDebug()<<"pot:"<<x+Paint.x()<<y+Paint.y();
QPoint pot;
pot.setX(ui->widget->x());
pot.setY(ui->widget->y());
// pot.setX(x+Paint.x());
// pot.setY(y+Paint.y());
// Left boundary
if(x+Paint.x() <= ui->widget->x())
{
pot.setX(ui->widget->x());
}else
{
pot.setX(x+Paint.x());
}
// Upper boundary
if(y+Paint.y() <= ui->widget->y())
{
pot.setY(ui->widget->y());
}else
{
pot.setY(y+Paint.y());
}
// Right border
if(x+Paint.x()+picWidth > ui->widget->width())
{
pot.setX(ui->widget->width()-picWidth);
}else
{
pot.setX(x+Paint.x());
}
// Lower boundary
if(y+Paint.y()+picHeight > ui->widget->height())
{
pot.setY(ui->widget->height()-picHeight);
}else
{
pot.setY(y+Paint.y());
}
if(!pic.isNull())
{
painter.drawTiledPixmap(pot.x(),pot.y(),picWidth,picHeight,QPixmap::fromImage(pic),0,0); // Draw a picture
painter.setPen(QPen(Qt::red,4));// Set brush form
painter.setBrush(QColor(255,0,0));
painter.drawEllipse(pot.x()+picWidth/2,pot.y()+picHeight/2,20,20); // Draw a picture
painter.setPen(QPen(Qt::blue,5));// Set brush form
QString text=QString::number(pot.x()+picWidth/2)+","+QString::number(pot.y()+picHeight/2);
painter.drawText(pot.x()+picWidth/2+30,pot.y()+picHeight/2+30,text ); // Draw a picture
// painter.drawPixmap(0,0,picWidth,picHeight,QPixmap::fromImage(pic));
}
}
bool Widget::event(QEvent *event)
{
// Whether the mouse is pressed down in the area
static bool bPress=false;
static QPoint pressPot; // Coordinates when the mouse is pressed down
if(event->type() == QEvent::MouseButtonPress )
{
QMouseEvent *mouse = dynamic_cast<QMouseEvent* >(event);
if(mouse->button()==Qt::LeftButton && Paint.contains(mouse->pos()))
{
bPress = true;
pressPot = mouse->pos();
// qDebug()<<"press pot:"<<mouse->x()<<","<<mouse->y()<<bPress;
}
}
else if(event->type() == QEvent::MouseButtonRelease )
{
QMouseEvent *mouse = dynamic_cast<QMouseEvent* >(event);
if(mouse->button()==Qt::LeftButton)
{
bPress=false;
// qDebug()<<"release pot:"<<mouse->x()<<","<<mouse->y();
}
}
// Move
if(event->type() == QEvent::MouseMove)
{
QMouseEvent *mouse = dynamic_cast<QMouseEvent* >(event);
// Some are pressed down
if(bPress)
{
// Get image offset
offset.setX(mouse->x() - pressPot.x());
offset.setY(mouse->y() - pressPot.y());
pressPot = mouse->pos();
action = Widget::Move;
this->update();
// qDebug()<<"move pot:"<<mouse->x()<<","<<mouse->y()<<bPress;
}
}
return QWidget::event(event);
}
// Analog keyboard
void Widget::on_pushButton_clicked()
{
action=Widget::Move;
offset.setX(-20) ;
offset.setY(-20) ;
this->update();
}
版权声明
本文为[Happinessคิดถึง]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220623156541.html
边栏推荐
- IDEA查看历史记录【文件历史和项目历史】
- 2022年P气瓶充装培训试题及模拟考试
- 《C语言程序设计》(谭浩强第五版) 第7章 用函数实现模块化程序设计 习题解析与答案
- PID debugging of coding motor (speed loop | position loop | follow)
- Super easy to use [general excel import function]
- Improvement of ref and struct in C 11
- After the mobile phone is connected to the computer, how can QT's QDIR read the mobile phone file path
- Flink real-time data warehouse project - Design and implementation of DWS layer
- Supersocket is Use in net5 - concept
- Explication détaillée des fonctions send () et recv () du programme Socket
猜你喜欢
一文了解全面静态代码分析
Drawing polygons with < polygon / > circular array in SVG tag
Course design of Database Principle -- material distribution management system
Fight leetcode again (290. Word law)
Node configuration environment CMD does not take effect
Quartz. Www. 18fu Used in net core
搭建XAMPP时mysql端口被占用
The most easy to understand service container and scope of dependency injection
Data mining series (3)_ Data mining plug-in for Excel_ Estimation analysis
MySQL之explain关键字详解
随机推荐
可以接收多种数据类型参数——可变参数
MySQL port is occupied when building xampp
Mysql database design specification
一文了解全面静态代码分析
The website JS in. Net core cefsharp chromium WebBrowser calls the C method in winfrom program
2022G2电站锅炉司炉考试题库及在线模拟考试
The most understandable life cycle of dependency injection
【无标题】
Oracle query foreign keys contain comma separated data
幂等性实践操作,基于业务讲解幂等性
Optimization of especially slow startup in idea debugging mode
MySql关键字GROUP_CONCAT,组合连接查询
MySql分组查询规则
. net 5 Web custom middleware implementation returns the default picture
Using swagger in. Net5
Chapter 7 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of modular programming exercises with functions
Test experience data
研讨会回放视频:如何提升Jenkins能力,使其成为真正的DevOps平台
Cefsharp stores cookies and reads cookies
JS recursive tree structure calculates the number of leaf nodes of each node and outputs it