当前位置:网站首页>Configuration software -- control drag and drop

Configuration software -- control drag and drop

2022-06-11 07:28:00 Fantastical Yu ruiyin

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
 Insert picture description here

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

原网站

版权声明
本文为[Fantastical Yu ruiyin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020521151503.html