|
|
| (同じ利用者による、間の1版が非表示) |
| 12行目: |
12行目: |
| QString strAppDir = QCoreApplication::applicationDirPath(); | | QString strAppDir = QCoreApplication::applicationDirPath(); |
| </syntaxhighlight> | | </syntaxhighlight> |
| | <br><br> |
| | |
| | == QIODevice::Textオプション == |
| | <code>QIODevice::Text</code>を指定する場合、改行コードは自動的にプラットフォームに合わせて変換される。<br> |
| <br><br> | | <br><br> |
|
| |
|
| 297行目: |
301行目: |
| v2 = 0x02; | | v2 = 0x02; |
| QByteArray data_4(std::begin<char>({0x00, v1, v2, v2, 0x05, 0x06, '\xf3'}), 7); | | QByteArray data_4(std::begin<char>({0x00, v1, v2, v2, 0x05, 0x06, '\xf3'}), 7); |
| </syntaxhighlight>
| |
| <br><br>
| |
|
| |
| == CSVファイル ==
| |
| 以下の例では、CSVファイルを作成している。<br>
| |
| 処理手順は以下の通りである。<br>
| |
| # 数値データを生成する。<br>ここでは、sin曲線y=sin(x)を生成する。
| |
| # 数値データをテキストデータに変換する。
| |
| # ファイル保存ダイアログを開いて、保存するファイル名を入力する。
| |
| # 全テキストデータを書き込む。
| |
| <br>
| |
| <syntaxhighlight lang="c++">
| |
| #include <QFileDialog>
| |
| #include <QMessageBox>
| |
| #include <QTextStream>
| |
| #include <QFile>
| |
| #include <vector>
| |
| #include <math.h>
| |
| #include <sstream>
| |
| #include "mainwindow.h"
| |
| #include "ui_mainwindow.h"
| |
|
| |
| using namespace std;
| |
|
| |
| MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
| |
| {
| |
| ui->setupUi(this);
| |
| }
| |
|
| |
| MainWindow::~MainWindow()
| |
| {
| |
| delete ui;
| |
| }
| |
|
| |
| void MainWindow::OnButtonClicked()
| |
| {
| |
| // Input File Name
| |
| QString FileName = QFileDialog::getSaveFileName(this, tr("Save CSV File"), "", tr("CSV (*.csv);;All Files (*)"));
| |
|
| |
| if(FileName.isEmpty())
| |
| { // Cancel
| |
| return;
| |
| }
| |
|
| |
| // Generate CSV Data
| |
| int iSize = 100;
| |
| vector<double> lx(iSize, 0.0f),
| |
| ly(iSize, 0.0f);
| |
|
| |
| double dx = 2 * 3.15159 / iSize;
| |
| for(int i = 0; i < iSize; i++)
| |
| {
| |
| lx[i] = i * dx;
| |
| ly[i] = sin(vx);
| |
| }
| |
|
| |
| // Convert from Double to String
| |
| QString strFileData = "";
| |
| //stringstream ss;
| |
| for(int i = 0; i < iSize; i++)
| |
| {
| |
| strFileData += lx[i] + ", " + ly[i] + "\n";
| |
| //ss << lx[i] << "," << ly[i] << endl;
| |
| }
| |
|
| |
| // Write to File
| |
| QFile File(FileName);
| |
|
| |
| if(!File.open(QIODevice::WriteOnly))
| |
| {
| |
| QMessageBox::information(this, tr("Unable to open file"), File.errorString());
| |
|
| |
| return;
| |
| }
| |
|
| |
| QTextStream OutStream(&File);
| |
| // 浮動小数点をe形式にする
| |
| //OutStream.setRealNumberNotation(QTextStream::ScientificNotation);
| |
|
| |
| // 浮動小数点の桁数を固定
| |
| //OutStream.setRealNumberNotation(QTextStream::FixedNotation);
| |
|
| |
| // 浮動小数点の桁数を設定
| |
| OutStream.setRealNumberPrecision(3);
| |
|
| |
| OutStream << strFileData;
| |
| //OutStream << ss.str().c_str();
| |
|
| |
| File.close();
| |
| }
| |
| </syntaxhighlight> | | </syntaxhighlight> |
| <br><br> | | <br><br> |