当前位置:网站首页>Configuration software -- control drag and drop
Configuration software -- control drag and drop
2022-06-11 07:28:00 【Fantastical Yu ruiyin】
Catalog
Configuration software – Control drag and drop
I have been using it recently Qt Make configuration software , At the beginning with QGraphics To do configuration software UI Design procedure , No kidding , To be honest, it looks a bit like , But there are big problems in the subsequent secondary development , So the second time is for reference feiyangqingyun Great God's attribute design idea , It can be said that it went very smoothly 10 Most of the work was finished in days .
First of all, let's show the effect 
Realization function
Control's drag, zoom, and select functions .
Related instructions
seen feiyangqingyun The students of related blogs should have found a problem , Build one by yourself SelectWidget Of QWidget Type of window , All relevant parameters are configured , But in the end Select It is impossible to drag a control by binding the corresponding control , But the zoom function really exists . Here you may realize feiyangqingyun When uploading the core code, delete some of the programs that make the control move , You need to add it by yourself .
Some of the wiser students here may think of feiyangqingyun One more movewidget A program that binds a control to make the control move , Isn't it perfect to add it . If you try , Control is bound SelectWidget and movewidget after ,movewidget It doesn't work at all . Come here , People who are used to code handling may panic , It is obvious that you need to study the program yourself and movewidget The mobile program in moves to Selectwidget In order to realize the function of control movement .
Two ways of thinking
The first way of thinking
Overlay controls on SelectWidget above
bool SelectWidget::eventFilter(QObject *obj, QEvent *event)// Here the program is a virtual function , Don't delete this program because it is empty
{
QMouseEvent *mouseEvent = (QMouseEvent *)event;
if(obj==widget&&widget!=nullptr&&move)
{
if (mouseEvent->type() == QEvent::MouseButtonPress) {
// If it is limited to drag with the left mouse button, judge whether it is the left mouse button at present
if (mouseEvent->button() != Qt::LeftButton) {
return false;
}
widgetpressed=true;
widgetpressed2=true;
selected=true;
// sendSelect(widget);
sendSelect();
lastPoint = mouseEvent->globalPos();
point4=this->pos();
point5=QPoint(point4.x()+this->width(),point4.y()+this->height());
} else if (mouseEvent->type() == QEvent::MouseMove && widgetpressed) {
// Calculate the coordinate offset , call move The function moves past
int offsetX = mouseEvent->globalPos().x() - lastPoint.x();
int offsetY = mouseEvent->globalPos().y() - lastPoint.y();
this->setGeometry(point4.x()+offsetX,point4.y()+offsetY,point5.x()-point4.x(),point5.y()-point4.y());
} else if (mouseEvent->type() == QEvent::MouseButtonRelease && widgetpressed) {
widgetpressed = false;
sendSelect2();
lastPoint=mouseEvent->globalPos();
}
return QWidget::eventFilter(obj,event);
}
else if(obj==this)
{
if (mouseEvent->type() == QEvent::MouseButtonPress) {
// If it is limited to drag with the left mouse button, judge whether it is the left mouse button at present
if(widgetpressed2==false)
{
emit presswidget();
widgetpressed2=false;
}
selected=true;
if (mouseEvent->button() == Qt::RightButton) {
}
// Determine whether the area of the control contains the coordinates of the current mouse
if (this->rect().contains(mouseEvent->pos())) {
//lastPoint = mouseEvent->pos();
QPoint point1=mouseEvent->pos();
point2=this->pos();
point3=QPoint(point2.x()+this->width(),point2.y()+this->height());
// This part of the program is commented out , Unstable function
/* if(point1.x()<20&&point1.y()<20) pressedarea=1;//qDebug()<<" Top left "<<endl; else if(point1.x()<this->width()*2/3&& point1.x()>this->width()/3&&point1.y()<20) pressedarea=2;// qDebug()<<" Middle up "<<endl; else if(point1.x()>point1.x()-20&&point1.y()<20) pressedarea=3;// qDebug()<<" The upper right "<<endl; else if(point1.x()<20&&point1.y()>this->height()/3&&point1.y()<this->height()*2/3)pressedarea=4;// qDebug()<<" Left center "<<endl; else if(point1.x()>this->width()-20&&point1.y()>this->height()/3&&point1.y()<this->height()*2/3)pressedarea=5;//qDebug()<<" Right middle "<<endl; else*/ if(point1.x()<20&&point1.y()>this->height()-20)pressedarea=6;// qDebug()<<" The lower left "<<endl;
else if(point1.x()<this->width()*2/3&& point1.x()>this->width()/3&&point1.y()>this->height()-20)pressedarea=7;//qDebug()<<" lower-middle "<<endl;
else if(point1.x()>point1.x()-20&&point1.y()>this->height()-20) pressedarea=8; //qDebug()<<" The lower right "<<endl;
else pressedarea=0;
pressed = true;
}
} else if (mouseEvent->type() == QEvent::MouseMove && pressed) {
// Calculate the coordinate offset , call move The function moves past
int offsetX = mouseEvent->pos().x();
int offsetY = mouseEvent->pos().y();
int x = point3.x()-point2.x()- offsetX;
/*if(pressedarea==1) { this->setGeometry(point2.x()+offsetX,point2.y()+offsetY,point3.x()-point2.x()-offsetX,point3.y()-point2.y()-offsetY); } else if(pressedarea==2) this->setGeometry(point2.x(),point2.y()+offsetY,point3.x()-point2.x(),point3.y()-point2.y()-offsetY); else if(pressedarea==3) this->setGeometry(point2.x(),point2.y()+offsetY,offsetX,point3.y()-point2.y()-offsetY); else if(pressedarea==4) this->setGeometry(point2.x()-(x-point3.x()+point2.x()),point2.y(),x,point3.y()-point2.y()); else if(pressedarea==5) this->setGeometry(point2.x(),point2.y(),offsetX,point3.y()-point2.y()); else*/ if(pressedarea==6)
this->setGeometry(point2.x()-(x-point3.x()+point2.x()),point2.y(),x,offsetY);
else if(pressedarea==7)
this->setGeometry(point2.x(),point2.y(),point3.x()-point2.x(),offsetY);
else if(pressedarea==8)
this->setGeometry(point2.x(),point2.y(),offsetX,offsetY);
} else if (mouseEvent->type() == QEvent::MouseButtonRelease && pressed) {
pressed = false;
//lastPoint=mouseEvent->globalPos();
}
}
return QWidget::eventFilter(obj,event);
}
This kind of program needs to change the parent Set to SelectWidget, Stack the controls directly onto SelectWidget above , In this way, the coordinates of the control are always relative to SelectWidget Of , Just move and zoom SelectWidget You can drag and zoom the control , It can be said to be extremely convenient . But the disadvantages are also very obvious , Later, when setting the control property sheet, the corresponding coordinates and length and width information of the control property cannot be obtained , You need to reset a control property sheet to set the control separately parent Coordinates and length and width information of , May affect the appearance and later read and write xml The file just needs to be correct xywh Do a separate treatment , But there's no denying it , The interface program designed by this method runs very smoothly , Unexpected BUG’ It's also very low . But this is not a mainstream approach , After all, some unnecessary amount of code will be added later , So the second is the more mainstream method .
The second method
In the same widget Next, force binding SelectWidget And the coordinates of the control .
bool SelectWidget::eventFilter(QObject *obj, QEvent *event)// Here the program is a virtual function , Don't delete this program because it is empty
{
QMouseEvent *mouseEvent = (QMouseEvent *)event;
if(obj==widget&&widget!=nullptr&&move)
{
if (mouseEvent->type() == QEvent::MouseButtonPress) {
// If it is limited to drag with the left mouse button, judge whether it is the left mouse button at present
if (mouseEvent->button() != Qt::LeftButton) {
return false;
}
widgetpressed=true;
widgetpressed2=true;
selected=true;
// sendSelect(widget);
sendSelect();
lastPoint = mouseEvent->globalPos();
point4=this->pos();
point5=QPoint(point4.x()+this->width(),point4.y()+this->height());
} else if (mouseEvent->type() == QEvent::MouseMove && widgetpressed) {
// Calculate the coordinate offset , call move The function moves past
int offsetX = mouseEvent->globalPos().x() - lastPoint.x();
int offsetY = mouseEvent->globalPos().y() - lastPoint.y();
this->setGeometry(point4.x()+offsetX,point4.y()+offsetY,point5.x()-point4.x(),point5.y()-point4.y());
widget->setGeometry(point4.x()+offsetX+5,point4.y()+offsetY+5,point5.x()-point4.x()-10,point5.y()-point4.y()-10);
} else if (mouseEvent->type() == QEvent::MouseButtonRelease && widgetpressed) {
widgetpressed = false;
sendSelect2();
lastPoint=mouseEvent->globalPos();
}
else if (event->type() == QEvent::Resize) {
// Set the current form size to follow the increase in the size of the form
this->setGeometry(widget->x()-5,widget->y()-5,widget->width()+10,widget->height()+10);
} else if (event->type() == QEvent::Move) {
// Move the current form to the offset
this->setGeometry(widget->x()-5,widget->y()-5,widget->width()+10,widget->height()+10);
}
return QWidget::eventFilter(obj,event);
}
else if(obj==this)
{
if (mouseEvent->type() == QEvent::MouseButtonPress) {
// If it is limited to drag with the left mouse button, judge whether it is the left mouse button at present
if(widgetpressed2==false)
{
emit presswidget();
widgetpressed2=false;
}
selected=true;
if (mouseEvent->button() == Qt::RightButton) {
}
// Determine whether the area of the control contains the coordinates of the current mouse
if (this->rect().contains(mouseEvent->pos())) {
//lastPoint = mouseEvent->pos();
QPoint point1=mouseEvent->pos();
point2=this->pos();
point3=QPoint(point2.x()+this->width(),point2.y()+this->height());
// This part of the program is commented out , Unstable function
/* if(point1.x()<20&&point1.y()<20) pressedarea=1;//qDebug()<<" Top left "<<endl; else if(point1.x()<this->width()*2/3&& point1.x()>this->width()/3&&point1.y()<20) pressedarea=2;// qDebug()<<" Middle up "<<endl; else if(point1.x()>point1.x()-20&&point1.y()<20) pressedarea=3;// qDebug()<<" The upper right "<<endl; else if(point1.x()<20&&point1.y()>this->height()/3&&point1.y()<this->height()*2/3)pressedarea=4;// qDebug()<<" Left center "<<endl; else if(point1.x()>this->width()-20&&point1.y()>this->height()/3&&point1.y()<this->height()*2/3)pressedarea=5;//qDebug()<<" Right middle "<<endl; else*/ if(point1.x()<20&&point1.y()>this->height()-20)pressedarea=6;// qDebug()<<" The lower left "<<endl;
else if(point1.x()<this->width()*2/3&& point1.x()>this->width()/3&&point1.y()>this->height()-20)pressedarea=7;//qDebug()<<" lower-middle "<<endl;
else if(point1.x()>point1.x()-20&&point1.y()>this->height()-20) pressedarea=8; //qDebug()<<" The lower right "<<endl;
else pressedarea=0;
pressed = true;
}
} else if (mouseEvent->type() == QEvent::MouseMove && pressed) {
selected=true;
// Calculate the coordinate offset , call move The function moves past
int offsetX = mouseEvent->pos().x();
int offsetY = mouseEvent->pos().y();
int x = point3.x()-point2.x()- offsetX;
/*if(pressedarea==1) { this->setGeometry(point2.x()+offsetX,point2.y()+offsetY,point3.x()-point2.x()-offsetX,point3.y()-point2.y()-offsetY); } else if(pressedarea==2) this->setGeometry(point2.x(),point2.y()+offsetY,point3.x()-point2.x(),point3.y()-point2.y()-offsetY); else if(pressedarea==3) this->setGeometry(point2.x(),point2.y()+offsetY,offsetX,point3.y()-point2.y()-offsetY); else if(pressedarea==4) this->setGeometry(point2.x()-(x-point3.x()+point2.x()),point2.y(),x,point3.y()-point2.y()); else if(pressedarea==5) this->setGeometry(point2.x(),point2.y(),offsetX,point3.y()-point2.y()); else*/ if(pressedarea==6)
this->setGeometry(point2.x()-(x-point3.x()+point2.x()),point2.y(),x,offsetY);
else if(pressedarea==7)
this->setGeometry(point2.x(),point2.y(),point3.x()-point2.x(),offsetY);
else if(pressedarea==8)
this->setGeometry(point2.x(),point2.y(),offsetX,offsetY);
} else if (mouseEvent->type() == QEvent::MouseButtonRelease && pressed) {
pressed = false;
//lastPoint=mouseEvent->globalPos();
}
}
return QWidget::eventFilter(obj,event);
}
After all, I compare dishes , In the program of setting coordinates and sizes, we use setGeometry, This may also make the program less fluent in execution , If necessary, you can optimize it yourself , Change to move and resize Two functions .
I am going to look for a job after graduation , This code may help me find a job , So I can only show part of it . If you really can't use these programs in the future , May be sent to github On .
Reference blog
https://qtchina.blog.csdn.net/article/details/100703610
边栏推荐
- Outer margin collapse
- MS office level II wrong question record [10]
- pycharm出现error.DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])
- R language Parallel Computing practice tutorial
- SQLZOO刷题记录-3
- big.js--使用/实例
- Directrix of ellipse
- Atomicinteger atomic operation class
- Senior openstacker - Bloomberg, vexxhost upgraded to the Gold member of openinfra Foundation
- Raspberry pie builds a full-featured NAS server (07): manage your library & read as you please
猜你喜欢

big.js--使用/实例

@Jsonproperty annotation

Education expert Mr. wangzhongze: family education focuses on self growth

一、SQLServer2008安装(带密码)、创建数据库、C#窗体项目测试

If you want to save an IP address, what data type is better? 99% of people will answer wrong!

软件测试周刊(第75期):唯有平视,才能看见真实的自己。

2022 low voltage electrician test questions and online simulation test
![[compilation principle] 05- syntax guided semantic computing -- Semantic Computing Based on translation mode](/img/7d/f3ba5a69e182160a5e1b51c7d9aaf9.png)
[compilation principle] 05- syntax guided semantic computing -- Semantic Computing Based on translation mode

May 30-June 5, 2022 AI industry weekly (issue 100): three years

Modular notes
随机推荐
Android and IOS reverse analysis / security detection / penetration testing framework
2022 low voltage electrician operation certificate test question simulation test platform operation
Seata的几种事务模式
MS office level II wrong question record [10]
The gap between the parent box and the child box
Miscellany C language
MS office level II wrong question record [8]
教育专家王中泽老师多年经验分享:家庭教育不是附庸品
Sdl-3 YUV playback
I/o multiplexing - select/poll/epoll
C language judging big end and small end [consortium or pointer] big end and small end conversion
2022年熔化焊接与热切割考试练习题及答案
Janus feature draft
Aiop introduction
Android和iOS逆向分析/安全检测/渗透测试框架
学 SQL 必须了解的10个高级概念
P5431 [template] multiplicative inverse 2
【Oracle 数据库】奶妈式教程day03 排序查询
Post-processing of ffmpeg miscellaneous notes
顶流编辑器 Atom,将于 12 月 15 日退出历史舞台