当前位置: 代码迷 >> 综合 >> C++ QT(Draw the curve from the import file)
  详细解决方案

C++ QT(Draw the curve from the import file)

热度:95   发布时间:2024-01-30 16:46:35.0

通过加载文件,解析文件中数据,并根据解析的数据进行绘制曲线。

  • Draw the curve by importing the file by loading the file, parsing the data in the file, and drawing the curve according to the parsed data.

importDataset 

void MainWindow::OnImportDataset()
{QString filepath = QFileDialog::getOpenFileName(NULL, "请选择数据文件", "", "TEMP(*.temp)");if (filepath.isEmpty()){QMessageBox::warning(NULL, "提示", "未选择数据文件", "确定");return;}else{tempfilepath = filepath;QFile f(filepath);if (!f.open(QIODevice::ReadOnly|QIODevice::Text))//打开指定文件{save_log.write_log(filepath+"文件导入失败!!!");}QTextStream tempInput(&f);QVector<QString> lineStr;while (!tempInput.atEnd()){//qDebug()<< tempInput.readLine();lineStr.push_back(tempInput.readLine());  //读取数据}f.close();//============================================================ui->tableWidget_dataset->clearContents();//只清除表格,不清除表头ui->tableWidget_dataset->setRowCount(lineStr.size());for (int i = 1 ; i < lineStr.size(); i++){if(lineStr[0].split('\t')[2] == "temp"){chartdata.push_back(lineStr[i].split('\t')[2].toDouble());ui->tableWidget_dataset->setItem(i-1,0,new QTableWidgetItem(lineStr[i].split('\t')[0]));ui->tableWidget_dataset->setItem(i-1,1,new QTableWidgetItem(lineStr[i].split('\t')[1]));ui->tableWidget_dataset->setItem(i-1,2,new QTableWidgetItem("---"));ui->tableWidget_dataset->setItem(i-1,3,new QTableWidgetItem(lineStr[i].split('\t')[2]));//居中显示ui->tableWidget_dataset->item(i-1,0)->setTextAlignment(Qt::AlignCenter);ui->tableWidget_dataset->item(i-1,1)->setTextAlignment(Qt::AlignCenter);ui->tableWidget_dataset->item(i-1,2)->setTextAlignment(Qt::AlignCenter);ui->tableWidget_dataset->item(i-1,3)->setTextAlignment(Qt::AlignCenter);}else if(lineStr[0].split('\t')[3] == "temp"){chartdata.push_back(lineStr[i].split('\t')[3].toDouble());ui->tableWidget_dataset->setItem(i-1,0,new QTableWidgetItem(lineStr[i].split('\t')[0]));ui->tableWidget_dataset->setItem(i-1,1,new QTableWidgetItem(lineStr[i].split('\t')[1]));ui->tableWidget_dataset->setItem(i-1,2,new QTableWidgetItem(lineStr[i].split('\t')[2]));ui->tableWidget_dataset->setItem(i-1,3,new QTableWidgetItem(lineStr[i].split('\t')[3]));//居中显示ui->tableWidget_dataset->item(i-1,0)->setTextAlignment(Qt::AlignCenter);ui->tableWidget_dataset->item(i-1,1)->setTextAlignment(Qt::AlignCenter);ui->tableWidget_dataset->item(i-1,2)->setTextAlignment(Qt::AlignCenter);ui->tableWidget_dataset->item(i-1,3)->setTextAlignment(Qt::AlignCenter);}}//===============================================================}
}

绘制曲线:

  • draw chart
void MainWindow::OnCreateChart()
{//=====================================================================//曲线绘制//=====================================================================ui->waveChart->setSmooth(false);ui->waveChart->setXStep(50);ui->waveChart->setSpace(40);qDebug()<< ui->waveChart->getYStep();auto MaxValue = std::max_element(std::begin(chartdata), std::end(chartdata));ui->waveChart->setMaxValue(MaxValue[0]);for (int i = 0 ; i < chartdata.size(); i++){ui->waveChart->addData(chartdata[i]);}
}

result: 

 

   I hope I can help you,If you have any questions, please  comment on this blog or send me a private message. I will reply in my free time.   

  相关解决方案