欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

(我的玩具)QT可拖动折线图控件

程序员文章站 2022-07-13 23:46:46
...

代码

环境:QT4.8.7 mingw编译器
功能 折线图 能够拖动 但不能缩放 下一步添加缩放功能
百度云盘链接
链接:https://pan.baidu.com/s/1IYbQhdGM9Z_UIExzjEqVpg
提取码:qwer
以下为代码

// mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include<QPixmap>
#include<QPoint>
#include<QString>
#include<QMouseEvent>
#include<QDebug>
#include<QList>
#include"cmath"
#include"myplot.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = NULL);
    ~MainWindow();
    Myplot *myplot;
private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;

};
#endif // MAINWINDOW_H
//myplot.h
#ifndef MYPLOT_H
#define MYPLOT_H
#include<QWidget>
#include<QPixmap>
#include<QPoint>
#include<QString>
#include<QMouseEvent>
#include<QDebug>
#include<QList>
#include"cmath"
class Myplot : public QWidget
{
    Q_OBJECT

public:
    Myplot(QWidget *parent = 0);
    ~Myplot();
    QPoint  coorToPix(double x,double y);
    QPoint  coorToPix(QPoint point);
    void addPoint(double x,double y);
private:

    QPixmap pixmap;
    void setXRange(double start,double stop,double step);
    void setYRange(double start,double stop,double step);
    bool isXRect(QPoint point);
    bool isYRect(QPoint point);
    void drawLine(QList<QPoint> line);
    QPoint prePoint;
    bool flag;
    double xLower;
    double xUper;
    double xStep;
    double dx;

    double yLower;
    double yUper;
    double yStep;
    double dy;

    double x_left;
    double x_right;
    double y_up;
    double y_down;
    QList<QPoint> points;

    void draw();
    QList<QPoint> computePoint(QPoint p1,QPoint p2);
    bool isRect(QPoint);
    bool isMid(QPoint p1,QPoint p2,QPoint mid);

protected:
    virtual void paintEvent ( QPaintEvent * event );
    virtual void mousePressEvent ( QMouseEvent * event );
    virtual void mouseMoveEvent ( QMouseEvent * event );
    virtual void mouseReleaseEvent ( QMouseEvent * event );
};
#endif // MYPLOT_H
//mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QPainter>
#include"myplot.h"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    myplot=new Myplot();
    ui->verticalLayout->addWidget(myplot);

}
MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    myplot->addPoint(550,60);
}

//myplot.cpp
#include "myplot.h"
#include<QPainter>
Myplot::Myplot(QWidget *parent)
    : QWidget(parent)
{
    flag=false;

    xLower=0;
    xUper=400;
    xStep=400/8;

    yLower=0;
    yUper=400;
    yStep=400/8;

    qDebug()<<"wh"<<xUper<<yUper;
    x_left=xLower;
    x_right=xUper;
    y_down=yLower;
    y_up=yUper;
    points.append(QPoint(-10,10));
    points.append(QPoint(200,500));
    points.append(QPoint(450,0));

    //points.append(QPoint(400,0));
    pixmap=QPixmap(500,500);
    pixmap.fill(Qt::white);
    setXRange(xLower,xUper,xStep);
    setYRange(yLower,yUper,yStep);
    draw();
    repaint();
}
Myplot::~Myplot()
{
}

void Myplot::paintEvent ( QPaintEvent * event ){
   QPainter painter(this);
   painter.drawPixmap(0,0,pixmap);
}
QPoint Myplot::coorToPix(double x,double y){
    return QPoint(x+40,400-y);
}
QPoint Myplot::coorToPix(QPoint point){
    double x=point.x()-x_left+40;
//    if(point.x()-ox+40<40){
//        x=40;
//    }
    double y=400-point.y()+y_down;
//    if(400-point.y()+oy>400){
//        y=400;
//    }
    return QPoint(x,y);
}
void Myplot::setXRange(double start,double stop,double step){

    QPainter painter;
    painter.begin(&pixmap);
    painter.drawLine(coorToPix(0,0),coorToPix(400,0));
    if(step<0)step=-step;
    x_left=start;
    x_right=stop;

    double title=start;
    int add=1;
    if(start<0){
        add=0;
    }
        ;
    if((int)start%(int)step!=0){
        title=(((int)start/(int)step)+add)*step;
    }
    qDebug()<<start<<stop<<step<<((int)start/(int)step)<<title;
    while(title<stop){
        painter.drawText(coorToPix(title-start,-10),QString::number(title));
        title=title+step;
    }
    painter.end();
}
void Myplot::setYRange(double start,double stop,double step){

    QPainter painter;
    painter.begin(&pixmap);
    painter.drawLine(coorToPix(0,0),coorToPix(0,400));
    if(step<0)step=-step;
    y_down=start;
    y_up=stop;
    double title=start;
    int add=1;
    if(start<0){
        add=0;
    }
        ;
    if((int)start%(int)step!=0){
        title=(((int)start/(int)step)+add)*step;
    }
    qDebug()<<start<<stop<<step<<((int)start/(int)step)<<title;
    while(title<stop){
        painter.drawText(coorToPix(-20,title-start),QString::number(title));
        title=title+step;
    }
    painter.end();
}
void Myplot::mousePressEvent ( QMouseEvent * event ){
    flag=true;
    prePoint=event->pos();
}
void Myplot::mouseMoveEvent ( QMouseEvent * event ){
    if(flag){
        dx=prePoint.x()-event->pos().x();
        dy=event->pos().y()-prePoint.y();


        pixmap=QPixmap(500,500);
        pixmap.fill(Qt::white);


        setXRange(xLower+dx,xUper+dx,xStep);
        setYRange(yLower+dy,yUper+dy,yStep);
        draw();
        repaint();
    }
}
void Myplot::mouseReleaseEvent ( QMouseEvent * event ){
    flag=false;
    xLower=xLower+dx;
    xUper=xUper+dx;

    yLower=yLower+dy;
    yUper=yUper+dy;



}
void Myplot::drawLine (QList<QPoint> line){
    QPainter painter;
    painter.begin(&pixmap);

    for(int i=0;i<line.count()-1;i++){
        painter.drawLine(coorToPix(line[i]),coorToPix(line[i+1]));
        qDebug()<<"line"<<line[i].x()<<line[i].y();
    }
    painter.end();
}
bool Myplot::isXRect(QPoint point){
    if(point.x()>=xLower&&point.x()<=xUper){

        return true;
    }
    qDebug()<<1;
    return false;
}
bool Myplot::isYRect(QPoint point){
    if(point.y()>=yLower&&point.x()<=yUper){

        return true;
    }
    qDebug()<<1;
    return false;
}
void Myplot::draw (){
    qDebug()<<"rect"<<x_left<<x_right<<y_down<<y_up;
    QList<QPoint> line;
    //一个点在里面前一个点或后一个点在外面
    for(int i=0;i<points.count();i++){
        if(isRect(points[i])){
            if(i-1>=0&&(!isRect(points[i-1]))){
                 qDebug()<<i;
                line.append(computePoint(points[i],points[i-1])[0]);
                qDebug()<<i;
            }
            line.append(points[i]);
            if(i+1<points.count()&&(!isRect(points[i+1]))){
                line.append(computePoint(points[i],points[i+1])[0]);
                qDebug()<<i;
            }

            qDebug()<<i;

        }else{

            drawLine(line);
            line.clear();
        }

    }
    drawLine(line);
    line.clear();
    //两个点都在外面但两个点形成的在里面
    for(int i=0;i<points.count()-1;i++){
        QPoint p1=points[i];
        QPoint p2=points[i+1];
        //double p1p2=sqrt(pow(p1.x()-p2.x(),2)+pow(p1.y()-p2.y(),2));
        QList<QPoint> ls=computePoint(p1,p2);
        if(ls.count()>1){
            line.append(ls[0]);
            line.append(ls[1]);
            drawLine(line);
            line.clear();
        }
    }

}
QList<QPoint> Myplot::computePoint(QPoint p1,QPoint p2){
    QList<QPoint> ls;
    double k=(double)(p1.y()-p2.y())/(p1.x()-p2.x());
    float y=(int)((x_left-p1.x())*k)+p1.y();
    QPoint left(x_left,y);
    y=(int)((x_right-p1.x())*k)+p1.y();
    QPoint right(x_right,y);

    float x=(int)((y_down-p1.y())/k)+p1.x();
    QPoint down(x,y_down);
    x=(int)((y_up-p1.y())/k)+p1.x();
    QPoint up(x,y_up);

    if(isRect(left)&&isMid(p1,left,p2))ls.append(left);
    if(isRect(right)&&isMid(p1,right,p2))ls.append(right);
    if(isRect(down)&&isMid(p1,down,p2))ls.append(down);
    if(isRect(up)&&isMid(p1,up,p2))ls.append(up);
    return ls;
}
bool Myplot::isRect(QPoint p1){
    if(p1.x()>=x_left&&p1.x()<=x_right&&p1.y()>=y_down&&p1.y()<=y_up){
        return true;
    }
    return false;
}
bool Myplot::isMid(QPoint p1,QPoint mid,QPoint p2){
    double p1mid=sqrt(pow(mid.x()-p1.x(),2)+pow(mid.y()-p1.y(),2));
    double p2mid=sqrt(pow(mid.x()-p2.x(),2)+pow(mid.y()-p2.y(),2));
    double p1p2=sqrt(pow(p1.x()-p2.x(),2)+pow(p1.y()-p2.y(),2));

    if(fabs(p1mid+p2mid-p1p2)>0.5){
        return false;
    }
    qDebug()<<mid.x()<<mid.y()<<p1mid<<p2mid<<p1p2;
    return true;
}
void Myplot::addPoint(double x, double y){

    points.append(QPoint(x,y));
    draw();
    repaint();
}

//main.cpp
#include "mainwindow.h"

#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

相关标签: qt4