2017-08-13 323 views
1

例如,在Visual Studio中有tChart及其Series屬性,它負責繪製圖形的行。以下是此代碼的一個示例如何使用QCustomPlot在Qt中繪製多行圖形

for (int j = 1; j < Nt - 1; j++) 
    { 
    for (int i = 1; i < Nt - 1; i++) 
    { 
     chart2->Series["" + (j + 1).ToString()]->Points->AddXY(i, wht[j][i]); 
    } 
    } 

並用很多行繪製此圖。

enter image description here

但我的任務是在Qt Creator transfering(因爲在Qt Creator可以做很多的機會) 此代碼

void MainWindow::drawdifnet(int Nt) 
{ 
    int N=Nt; 
    int N1=pow(N,2); 
    QVector<double> x22(N), y22(N1); 
    int ii=0,jj=0; 
    for (int j = 0; j < Nt ; j++) 
      { 
       for (int i = 0; i < Nt ; i++) 
       {   
        x22[jj]=i; 
        y22[ii]=wht[j][i]; 
        ui->widget_2->addGraph(); 
        ui->widget_2->graph(0)->setData(x22,y22); 
        ii++; 
       } 
       jj++; 
    } 
    ui->widget_2->xAxis->setLabel("OsX"); 
    ui->widget_2->yAxis->setLabel("OsY"); 
    ui->widget_2->xAxis->setRange(30,30); 
    ui->widget_2->replot(); 
} 

無法正常工作。 結果是空的小部件

與幫助調試器會首先我檢查QVectors數據 在這個圖片中看到我的DINAMIC陣列wht[j][i]在工作和QVector加載yy[ii]

enter image description here enter image description here

我想循環中的問題。

在QtCustomPlot教程這個問題的解決這個代碼

ui->widget_2->graph(0)->setData(x,y); 
ui->widget_2->graph(1)->setData(x11,y11); 
ui->widget_2->graph(2)->setData(x22,y22); 

但在我的處境行的數量是知道什麼時候該程序的工作。

如何創建和分配我的數組

void created(int Nt, int Nx) ///This function creating my dynamic array 
{ 
    wht = new double *[Nt]; 
    for (int i = 0; i < Nt; i++) 
     wht[i] = new double[Nx]; 
} 

inline double fn(int T, double x) ///these 4 functions for my mathematical part(works good) 
{ 
    if (x >= 0) 
     return T; 
    return 0; 
} 

inline double u0(int T, double x) 
{ 
    return fn(T, x); 
} 

inline double u1(int T, double a, int xmin, double t) 
{ 
    return fn(T, xmin - a * t); 
} 

inline double u2(int T, double a, int xmax, double t) 
{ 
    return fn(T, xmax - a * t); 
} 


void calculatedifnet(int xmin, double hx, double ht, double a, int Nx, int Nt, int T) 
//These main function.We have the empty array and in this function we fill array. Then we solve in the main loop and the fill the first indexes wht[j] 
{ 
    for (int i = 0; i < Nt; i++) 
    { 
     wht[0][i] = u0(T, xmin + i*hx);//fill the second indexeswht[null][i] 
    } 

    for (int j = 0; j < Nt - 1; j++)//the calculated code(works right).The result writing in wht[j] 
    { 
     wht[j + 1][0] = u1(T, a, xmin, j*ht); 
     for (int i = 1; i < Nt; i++) 
     { 
      double dudx = (wht[j][i] - wht[j][i - 1])/hx; 
      wht[j + 1][i] = -a * dudx * ht + wht[j][i]; 
     } 
    } 
} 
+0

爲什麼你的載體之一是大小N和其他N ** 2 = N×N個? – eyllanesc

+0

在ln迭代中,j'計算12點。 每一步'j'包括12步計算座標。 '12圖表= 12點'在每張圖表中,因此有不同的大小。 – beginner

+0

wht的數據類型是什麼? – eyllanesc

回答

3

在代碼中有以下錯誤:

  • 如果我們觀察x是一個常矢量從0Nt-1,那麼我們只有創建一次:

QVector<double> x(Nt); 
for (int i = 0; i < Nt ; i++) 
    x[i]=i;//0 to Nt-1 
  • addGraph()增加了一個圖,把它放在最後的位置,如果你想圖表必須通過最後一個索引來訪問,而不是通過索引0:

ui->widget_2->addGraph()->setData(xx, yy); 
  • 假設wht類型爲QVector<QVector<double>>,大小爲NtxNt,則不需要訪問每個元素,我們可以訪問每個QVector<double>,因爲函數setData()接受這種類型的數據作爲輸入。該功能setData()你必須通過相同大小的2個載體,但你傳遞的NtNt*Nt 2個載體,這產生了警告:

ui->widget_2->addGraph()->setData(x, wht[j]); 
  • setRange()地方的範圍從到b,但如果它們是相同的QCustomPlot永遠不會適合的範圍內,爲我的測試我設置如下:

ui->widget_2->xAxis->setRange(0,Nt); 
ui->widget_2->yAxis->setRange(0,Nt*Nt); 

總之代碼將如下所示:

void MainWindow::drawdifnet(int Nt){ 

    QVector<double> x(Nt); 
    for (int i = 0; i < Nt ; i++) 
     x[i]=i;//0 to Nt-1 

    for (int j = 0; j < Nt ; j++) 
     ui->widget_2->addGraph()->setData(x, wht[j]); 

    /* if c++11 
    for (auto& row: wht) 
     ui->widget_2->addGraph()->setData(x, row); 
    */ 

    ui->widget_2->xAxis->setLabel("OsX"); 
    ui->widget_2->yAxis->setLabel("OsY"); 
    ui->widget_2->xAxis->setRange(0,Nt); 
    ui->widget_2->yAxis->setRange(0,Nt*Nt); 
    ui->widget_2->replot(); 

} 

輸出:

enter image description here

注:對於測試wht[i][j] = i*j

在你的情況wht是變量類型爲double** ,還假設Nx>=Nt,爲此,你必須使用下面的代碼:

void MainWindow::drawdifnet(int Nt) 
{ 
    QVector<double> x(Nt); 
    for (int i = 0; i < Nt ; i++){ 
     x[i]=i;//0 to Nt-1 
    } 

    QVector<double> y(Nt); 
    for(int i=0; i<Nt; i++){ 
     for(int j=0; j<Nt; j++){ 
      y[j] = wht[i][j]; 
     } 
     ui->widget_2->addGraph()->setData(x, y); 
    } 

    ui->widget_2->xAxis->setLabel("OsX"); 
    ui->widget_2->yAxis->setLabel("OsY"); 
    ui->widget_2->xAxis->setRange(0,12); 
    ui->widget_2->yAxis->setRange(0,3.5); 
    ui->widget_2->replot(); 
} 

輸入:

  • 創建(12,12);
  • calculatedifnet(1,5.5,0.9,12,12,3);

輸出:

enter image description here

+0

在此代碼錯誤 'UI-> widget_2-> addGraph() - >使用setData(X,WHT [J]);' '爲調用「QCPGraph ::使用setData(QVector &,雙不匹配函數*&)'ui-> widget_2-> addGraph() - > setData(x,wht [j]); ' – beginner

+0

注意:對於測試,我寫ui-> widget_2-> addGraph() - > setData(x,i * j); – beginner