//main.cpp
#include <QApplication>
#include <QFont>
#include <QPushButton>
#include <QWidget>
#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <QFileDialog>
#include <QStringList>
#include <QGridLayout>
#include <QVBoxLayout>
#include <qwt_plot_grid.h>
#include <qwt_legend.h>
#include <QDebug>
#include <QObject>
#include "button.h"
int main(int argc, char *argv[])
{ const int anz=50;
double *x1;
double *y1;
double *x2;
double *y2;
x1 = new double[50];
y1 = new double[50];
x2 = new double[50];
y2 = new double[50];
for (int i=0;i<anz;i++){
x1[i]= i;
y1[i]= 3*i;
x2[i]= i;
y2[i]= 2*i;
}
window.resize(800, 800);
OpenFile *open = new OpenFile("Open", &window);
quit.setGeometry(10, 450, 100, 40);
QObject::connect(&quit,
SIGNAL(clicked
()),
&app,
SLOT(quit
()));
copy.setGeometry(150, 450, 100, 40);
QObject::connect(©,
SIGNAL(clicked
()), open,
SLOT(CopyArray
(x1,y1
)));
.
.
.
grid2->addWidget(myPlot, 0,0,4,4);
grid2->addWidget(&quit, 5,0);
grid2->addWidget(open, 5,3);
grid2->addWidget(©, 5,1);
window.setLayout(grid2);
window.show();
return app.exec();
}
//main.cpp
#include <QApplication>
#include <QFont>
#include <QPushButton>
#include <QWidget>
#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <QFileDialog>
#include <QStringList>
#include <QGridLayout>
#include <QVBoxLayout>
#include <qwt_plot_grid.h>
#include <qwt_legend.h>
#include <QDebug>
#include <QObject>
#include "button.h"
int main(int argc, char *argv[])
{ const int anz=50;
double *x1;
double *y1;
double *x2;
double *y2;
x1 = new double[50];
y1 = new double[50];
x2 = new double[50];
y2 = new double[50];
for (int i=0;i<anz;i++){
x1[i]= i;
y1[i]= 3*i;
x2[i]= i;
y2[i]= 2*i;
}
QApplication app(argc, argv);
QWidget window;
window.resize(800, 800);
OpenFile *open = new OpenFile("Open", &window);
QPushButton quit("Quit", &window);
quit.setFont(QFont("Times", 18, QFont::Bold));
quit.setGeometry(10, 450, 100, 40);
QObject::connect(&quit, SIGNAL(clicked()), &app, SLOT(quit()));
QPushButton copy("Copy", &window);
copy.setFont(QFont("Times", 18, QFont::Bold));
copy.setGeometry(150, 450, 100, 40);
QObject::connect(©, SIGNAL(clicked()), open, SLOT(CopyArray(x1,y1)));
QwtPlot *myPlot = new QwtPlot(&window);
.
.
.
QGridLayout *grid2 = new QGridLayout;
grid2->addWidget(myPlot, 0,0,4,4);
grid2->addWidget(&quit, 5,0);
grid2->addWidget(open, 5,3);
grid2->addWidget(©, 5,1);
window.setLayout(grid2);
window.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
// button.h
#ifndef BUTTON_H
#define BUTTON_H
#include <QApplication>
#include <QFont>
#include <QPushButton>
#include <QWidget>
#include <QFileDialog>
#include <QStringList>
#include <QDebug>
#include <QObject>
#include <QFile>
{
Q_OBJECT
public:
public slots:
void CopyArray(double* xe, double* ye);
private:
double *x, *y;
private slots:
void on_pushButton_clicked();
};
#endif // button.h
// button.h
#ifndef BUTTON_H
#define BUTTON_H
#include <QApplication>
#include <QFont>
#include <QPushButton>
#include <QWidget>
#include <QFileDialog>
#include <QStringList>
#include <QDebug>
#include <QObject>
#include <QFile>
class OpenFile:public QPushButton
{
Q_OBJECT
public:
OpenFile(QString name, QWidget * parent);
public slots:
void CopyArray(double* xe, double* ye);
private:
double *x, *y;
private slots:
void on_pushButton_clicked();
};
#endif // button.h
To copy to clipboard, switch view to plain text mode
//button.cpp
#include "button.h"
{
setGeometry(150, 450, 100, 40);
QObject::connect(this,
SIGNAL(clicked
()),
this,
SLOT(on_pushButton_clicked
()));
}
void OpenFile::on_pushButton_clicked()
{
"",
tr("TXT file (*.txt)"));
qDebug() << fileName;
if( !file.
open( QIODevice::ReadOnly ) ) //open file for reading return; //error
QString fileData
= file.
readAll();
//read all to byte array
file.close(); //close file
strList = fileData.split( "," ); //create number list as strings
qDebug() << strList << strList.count(); //print qtway what's inside baData
QList< double > dDoubleList;
bool ok;
for( int i = 0; i < strList.count(); ++i )
{
dDoubleList.append( strList[i].toInt( &ok ) );
qDebug() << ok; //see if error - false = error , true = OK
}
this->x = new double[strList.count()];
this->y = new double[strList.count()];
int j=0;
for (int i=0; i< strList.count(); i=i+2)
{ this->x[j]=dDoubleList[i];
this->y[j]=dDoubleList[i+1];
j++;
}
qDebug() << x[0] << y[0] << x[1] << y[1];
}
void OpenFile::CopyArray(double* xe, double* ye)
{
xe = this->x;
ye = this->y;
qDebug() << xe[0] << ye[0] << xe[1] << ye[1];
}
//button.cpp
#include "button.h"
OpenFile::OpenFile(QString name, QWidget * parent)
:QPushButton(name, parent)
{
setFont(QFont("Times", 18, QFont::Bold));
setGeometry(150, 450, 100, 40);
QObject::connect(this, SIGNAL(clicked()), this, SLOT(on_pushButton_clicked()));
}
void OpenFile::on_pushButton_clicked()
{
QString fileName = QFileDialog::getOpenFileName(this, tr("Open TXT File"),
"",
tr("TXT file (*.txt)"));
qDebug() << fileName;
QFile file( fileName );
if( !file.open( QIODevice::ReadOnly ) ) //open file for reading
return; //error
QString fileData = file.readAll(); //read all to byte array
file.close(); //close file
QStringList strList;
strList = fileData.split( "," ); //create number list as strings
qDebug() << strList << strList.count(); //print qtway what's inside baData
QList< double > dDoubleList;
bool ok;
for( int i = 0; i < strList.count(); ++i )
{
dDoubleList.append( strList[i].toInt( &ok ) );
qDebug() << ok; //see if error - false = error , true = OK
}
this->x = new double[strList.count()];
this->y = new double[strList.count()];
int j=0;
for (int i=0; i< strList.count(); i=i+2)
{ this->x[j]=dDoubleList[i];
this->y[j]=dDoubleList[i+1];
j++;
}
qDebug() << x[0] << y[0] << x[1] << y[1];
}
void OpenFile::CopyArray(double* xe, double* ye)
{
xe = this->x;
ye = this->y;
qDebug() << xe[0] << ye[0] << xe[1] << ye[1];
}
To copy to clipboard, switch view to plain text mode
Bookmarks