当前位置:网站首页>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
边栏推荐
- @Jsonproperty annotation
- 【编译原理】05-语法制导的语义计算——基于翻译模式的语义计算
- Sdl-3 YUV playback
- big.js--使用/实例
- 【Oracle 数据库】奶妈式教程day02 数据库管理工具SQLPLUS的使用
- Calculate the day of the week for a specific month, year and day
- Pat class A by category
- Console program for viewing BMP files
- 1190. invert the substring between each pair of parentheses
- [analysis of STL source code] summary notes (6): Design of iterator and magical traits
猜你喜欢

JVM Learning record (7) - - class Loading Process and parent delegation Model
![[analysis of STL source code] summary notes (6): Design of iterator and magical traits](/img/57/eaf02e880d205c5912f353609c9520.png)
[analysis of STL source code] summary notes (6): Design of iterator and magical traits

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

2022 melting welding and thermal cutting exam exercises and answers

CRMEB/V4.4标准版打通版商城源码小程序公众号H5+App商城源码

1、 Sqlserver2008 installation (with password), database creation, C form project test

Post-processing of ffmpeg miscellaneous notes

QT interface nested movement based on qscrollarea

2、 User login and registration

软件测试周刊(第75期):唯有平视,才能看见真实的自己。
随机推荐
[STL source code analysis] summary notes (10): hashtable exploration
[STL source code analysis] summary notes (5): a good helper for understanding iterators --list
pycharm出现error.DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])
Compound ratemodel contract analysis
1190. invert the substring between each pair of parentheses
What is the lifecycle of automated testing?
2022低压电工考题及在线模拟考试
What is the difference between gaussdb for redis and redis?
[STL source code analysis] summary notes (7): ingenious deque
【CF#388 (Div. 2)】A. Bachgold Problem
The maximum number of divisors of numbers in the int range is 1536
AtomicInteger原子操作类
Experience record of rural housing integration script
Interview question 17.08 Circus tower
SQLZOO刷题记录-3
Education expert wangzhongze solves students' problems with one move
Software testing weekly (issue 75): only when you look down, can you see your true self.
@Jsonproperty annotation
CRMEB/V4.4标准版打通版商城源码小程序公众号H5+App商城源码
Ffmpe a small demo to understand 80% of common APIs