关于Qt 动态画 折线图

萨斯辈的呼唤 发布于 2013/12/10 14:36
阅读 2K+
收藏 0

自己写了一个动态画折线图,不是卡死就是崩溃。

     QList<int> list_temp;

    list_temp<<10<<20<<30<<20<<50<<80<<10;

 int x=20;
void QPaintArea::paintEvent(QPaintEvent *)
{
    QPainter painter(this); //创建QPainter一个对象
     painter.drawLine(20,20,10,30);
    painter.drawLine(20,20,30,30);
    painter.drawLine(20,20,20,280);
    painter.drawLine(20,280,280,280);
    painter.drawLine(280,280,270,270);
    painter.drawLine(280,280,260,300);
    painter.drawText(0,20,"100cm");
    painter.drawText(10,290,"0cm/t");

    QPainter lpainter(this); //创建QPainter一个对象
    QPen pen;
    pen.setColor(Qt::black);
    lpainter.setPen(pen);
    lpainter.setPen(pen);
    lpainter.setRenderHint(QPainter::Antialiasing, true);
    lpainter.setPen(QPen(Qt::black, 12, Qt::DashDotLine, Qt::RoundCap));
    lpainter.setBrush(QBrush(Qt::green, Qt::SolidPattern));
    lpainter.setPen(Qt::red);


    QList<int>::iterator i;
    QList<int>::iterator j;
    for(i = list_temp.begin(); i != list_temp.end();++i)
    {
        qDebug()<<*i;
        int temp1 = *i;
        j = i.operator ++(1);
        int temp2 = *j;
        lpainter.drawLine(x,temp1,x+20,temp2);
        x=x+20;
    }
    Sleep(1000);
    update();
}

如果说、有好的例子,能分享给我看看,谢谢大家。

加载中
0
beyondforever68
beyondforever68

没看懂你for循环中的意思,我是按照类似java中的用法改的,你看看是不是这个意思

我这里是没出错的。

#include "qpaintarea.h"
#include <QPainter>
#include <QList>

#include <QDebug>

QList<int> list_temp;

QPaintArea::QPaintArea(QWidget *parent) :
    QWidget(parent)
{
    list_temp<<10<<20<<30<<20<<50<<80<<10;
}
void QPaintArea::paintEvent(QPaintEvent *){
    QPainter painter(this); //创建QPainter一个对象
    painter.drawLine(20,20,10,30);
    painter.drawLine(20,20,30,30);
    painter.drawLine(20,20,20,280);
    painter.drawLine(20,280,280,280);
    painter.drawLine(280,280,270,270);
    painter.drawLine(280,280,260,300);
    painter.drawText(0,20,"100cm");
    painter.drawText(10,290,"0cm/t");

    QPainter lpainter(this); //创建QPainter一个对象
    QPen pen;
    pen.setColor(Qt::black);
    lpainter.setPen(pen);
    lpainter.setPen(pen);
    lpainter.setRenderHint(QPainter::Antialiasing, true);
    lpainter.setPen(QPen(Qt::black, 12, Qt::DashDotLine, Qt::RoundCap));
    lpainter.setBrush(QBrush(Qt::green, Qt::SolidPattern));
    lpainter.setPen(Qt::red);


//    QList<int>::iterator i;
//    QList<int>::iterator j;
//    for(i = list_temp.begin(); i != list_temp.end();++i)
//    {
//        qDebug()<<*i;
//        int temp1 = *i;
//        j = i.operator ++(1);
//        int temp2 = *j;
//        lpainter.drawLine();
//        lpainter.drawLine(x,temp1,x+20,temp2);
//        x=x+20;
//    }
//    Sleep(1000);
    int x = 20;
    for(int i = 0; i < list_temp.size() - 1; i++){
        int temp1 = list_temp.at(i);
        int temp2 = list_temp.at(i + 1);
        lpainter.drawLine(x, temp1, x + 20, temp2);
        x += 20;
    }
    update();
}



0
萨斯辈的呼唤
萨斯辈的呼唤

引用来自“beyondforever68”的答案

没看懂你for循环中的意思,我是按照类似java中的用法改的,你看看是不是这个意思

我这里是没出错的。

#include "qpaintarea.h"
#include <QPainter>
#include <QList>

#include <QDebug>

QList<int> list_temp;

QPaintArea::QPaintArea(QWidget *parent) :
    QWidget(parent)
{
    list_temp<<10<<20<<30<<20<<50<<80<<10;
}
void QPaintArea::paintEvent(QPaintEvent *){
    QPainter painter(this); //创建QPainter一个对象
    painter.drawLine(20,20,10,30);
    painter.drawLine(20,20,30,30);
    painter.drawLine(20,20,20,280);
    painter.drawLine(20,280,280,280);
    painter.drawLine(280,280,270,270);
    painter.drawLine(280,280,260,300);
    painter.drawText(0,20,"100cm");
    painter.drawText(10,290,"0cm/t");

    QPainter lpainter(this); //创建QPainter一个对象
    QPen pen;
    pen.setColor(Qt::black);
    lpainter.setPen(pen);
    lpainter.setPen(pen);
    lpainter.setRenderHint(QPainter::Antialiasing, true);
    lpainter.setPen(QPen(Qt::black, 12, Qt::DashDotLine, Qt::RoundCap));
    lpainter.setBrush(QBrush(Qt::green, Qt::SolidPattern));
    lpainter.setPen(Qt::red);


//    QList<int>::iterator i;
//    QList<int>::iterator j;
//    for(i = list_temp.begin(); i != list_temp.end();++i)
//    {
//        qDebug()<<*i;
//        int temp1 = *i;
//        j = i.operator ++(1);
//        int temp2 = *j;
//        lpainter.drawLine();
//        lpainter.drawLine(x,temp1,x+20,temp2);
//        x=x+20;
//    }
//    Sleep(1000);
    int x = 20;
    for(int i = 0; i < list_temp.size() - 1; i++){
        int temp1 = list_temp.at(i);
        int temp2 = list_temp.at(i + 1);
        lpainter.drawLine(x, temp1, x + 20, temp2);
        x += 20;
    }
    update();
}



可能是我容器的这块运用出现了问题,但是=。=结果好像还是不对。。。没有实现我想要的
返回顶部
顶部