当前位置:网站首页>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
边栏推荐
- How does Microsoft solve the problem of multiple programs on PC side -- internal implementation
- Five tips for cross-border e-commerce in 2022
- 批量下载文件----压缩后再下载
- 2022年做跨境电商五大技巧小分享
- Test experience data
- The most understandable life cycle of dependency injection
- 【VS Code】解决jupyter文件在vs code中显示异常的问题
- Configuration table and page information automatically generate curd operation page
- The website JS in. Net core cefsharp chromium WebBrowser calls the C method in winfrom program
- Idea view history [file history and project history]
猜你喜欢

A set of combination boxing to create an idea eye protection scheme

手机连接电脑后,QT的QDIR怎么读取手机文件路径
![Use of ADB command [1]](/img/e6/fb8fd6956c226f75b831f8eb46277f.png)
Use of ADB command [1]

IOTOS物联中台对接海康安防平台(iSecure Center)门禁系统

【无标题】

可以接收多種數據類型參數——可變參數

Top 9 task management system in 2022

12. < tag linked list and common test site synthesis > - lt.234 palindrome linked list

Mise en service PID du moteur de codage (anneau de vitesse | anneau de position | suivant)

Is it difficult to choose binary version control tools? After reading this article, you will find the answer
随机推荐
New ORM framework -- Introduction to beetlsql
队列的存储和循环队列
Idea view history [file history and project history]
Experiment 5 components and event handling
Course design of Database Principle -- material distribution management system
Iotos IOT middle platform is connected to the access control system of isecure center
Swap the left and right of each node in a binary tree
软件测试相关知识~
poi根据数据创建导出excel
幂等性实践操作,基于业务讲解幂等性
IDEA查看历史记录【文件历史和项目历史】
Using swagger in. Net5
. NETCORE sets the API post mode, which can accept parameters directly in parentheses
全新的ORM框架——BeetlSQL介绍
The most easy to understand service container and scope of dependency injection
Mysql database, inconsistent index character set, slow SQL query, interface timeout
Supersocket is Use in net5 - concept
Log4net is in Net core usage
It can receive multiple data type parameters - variable parameters
超好用的Excel异步导出功能