• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

Qt绘图面板中的对象拖拽

开发技术 开发技术 2周前 (05-01) 7次浏览

Qt绘图面板中的对象拖拽

qdrawwidget.h

#ifndef QDRAWWIDGET_H
#define QDRAWWIDGET_H

#include <QObject>
#include <QWidget>
#include <QFrame>
#include <QPainter>
#include <QMouseEvent>
#include <QDebug>

class QDrawWidget : public QWidget
{
    Q_OBJECT
public:
    explicit QDrawWidget(QWidget *parent = nullptr);

    void drawGrid();

protected:
    void paintEvent(QPaintEvent *);

    void mousePressEvent(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);

signals:

public slots:

private:
    int win_width=0;
    int win_height = 0;

     //QPainter painter;

    int startX = 0;
    int startY = 0;
    int endX = 0;
    int endY = 0;

    int totalX=0;
    int totalY=0;

    int rect1_x = 122;
    int rect1_y = 122;

    int rect2_x = 522;
    int rect2_y = 122;

    int rect3_x = 922;
    int rect3_y = 122;

};

#endif // QDRAWWIDGET_H

 

qdrawwidget.cpp

#include "qdrawwidget.h"

QDrawWidget::QDrawWidget(QWidget *parent) : QWidget(parent)
{

    win_width = this->geometry().width();
    win_height = this->geometry().height();

    this->setStyleSheet("border:2px solid red;");




}



void QDrawWidget::drawGrid()
{
    //repaint();
    update();
}




void QDrawWidget::paintEvent(QPaintEvent *)
{
    

    int s_x = endX-startX;
    int s_y = endY-startY;



    //totalX = totalX+s_x;
    //totalY = totalY+s_y;

    totalX = s_x;
    totalY = s_y;

    rect1_x=rect1_x+s_x;
    rect1_y=rect1_y+s_y;

    rect2_x=rect2_x+s_x;
    rect2_y=rect2_y+s_y;

    rect3_x=rect3_x+s_x;
    rect3_y=rect3_y+s_y;

    //qDebug()<<""<<rect1_x<<"       "<<rect1_y<<"    "<<rect2_x<<"       "<<rect2_y<<"     "<<s_x<<"       "<<s_y;
    qDebug()<<""<<rect1_x<<"       "<<rect1_y<<"    "<<"     "<<s_x<<"       "<<s_y;



    int win_width = this->geometry().width();
    int win_height = this->geometry().height();

    QPainter painter(this);

    qDebug()<<"win size:"<<win_width<<"   "<<win_height;



    for(int x=0;x<win_width;x+=25)
    {
        painter.drawLine(QPoint(x,1),QPoint(x,win_height));
    }

    for(int y = 0;y<win_height;y+=25)
    {
        painter.drawLine(QPoint(1,y),QPoint(win_width,y));
    }


    QColor color_red(255,0,0);//设置颜色
    QPen pen1(color_red);//定义画笔
    pen1.setWidth(6);
    painter.setPen(pen1);
    painter.drawRect(rect1_x,rect1_y,200,200);//绘制矩形

    QColor color_green(0,255,0);//设置颜色
    QPen pen2(color_green);//定义画笔
    pen2.setWidth(4);
    painter.setPen(pen2);
    painter.drawRect(rect2_x,rect2_y,200,200);//绘制矩形


    QColor color_yellow(0,150,255);//设置颜色
    QPen pen3(color_yellow);//定义画笔
    pen3.setWidth(4);
    painter.setPen(pen3);
    painter.drawRect(rect3_x,rect3_y,200,200);//绘制矩形

    /*
    startX=0;
    startY=0;
    endX=0;
    endY=0;
    */

    startX = endX;
    startY = endY;


}


void QDrawWidget::mousePressEvent(QMouseEvent *event)
{

    if(event->button() == Qt::LeftButton)
    {
        //int x = this->pos().x();
        //int y = this->pos().y();
        int x = event->windowPos().x();
        int y = event->windowPos().y();
        startX = x;
        startY = y;


        qDebug()<<"p: "<<x<<"   "<<y;
    }

}

void QDrawWidget::mouseMoveEvent(QMouseEvent *event)
{

    if(event->buttons() & Qt::LeftButton)
    {
        int x = event->windowPos().x();
        int y = event->windowPos().y();

        endX = x;
        endY = y;
        qDebug()<<"m: "<<x<<"   "<<y;

        update();
    }


}

void QDrawWidget::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        int x = event->windowPos().x();
        int y = event->windowPos().y();

        endX = x;
        endY = y;
        qDebug()<<"r: "<<x<<"   "<<y;
    }

    update();
}


//////////////////

 

Qt绘图面板中的对象拖拽

 

Qt绘图面板中的对象拖拽

 

Qt绘图面板中的对象拖拽

 

Qt绘图面板中的对象拖拽

 

Qt绘图面板中的对象拖拽

 

 

 

############################3


程序员灯塔
转载请注明原文链接:Qt绘图面板中的对象拖拽
喜欢 (0)