I need to create a family of plots during a test of my program. I want to use QCustomPlot
for such purposes, but I have a problem. For creating and saving my plot in the file, I have to create QApplication
, after I need to reimplement my class from QMainWindow
and after that, I can Use my QCustomPlot
in the function that creates all needed plots and saves them in to file. Is it possible to avoid creating QApplication
instance or QMainWindow
instance or them both?
See code of my function below:
void MainWindow::print_image(std::vector<double> az, std::vector<double> el, std::vector<double> za, char *image_path){
QVector<double> azFunct;// =// QVector<double>::fromStdVector(az);
QVector<double> elFunct;// =// QVector<double>::fromStdVector(el);
QVector<double> zFunct = QVector<double>::fromStdVector(za);
QVector<double> xAxisValue(az.size());
//Filling axes of x with values
std::iota(xAxisValue.begin(),xAxisValue.end(),0);
QCustomPlot customPlot;
customPlot.setLocale(QLocale(QLocale::English, QLocale::UnitedKingdom)); // period as decimal separator and comma as thousand separator
customPlot.legend->setVisible(true);
QFont legendFont = font(); // start out with MainWindow's font..
legendFont.setPointSize(9); // and make a bit smaller for legend
customPlot.legend->setFont(legendFont);
customPlot.legend->setBrush(QBrush(QColor(255,255,255,230)));
// by default, the legend is in the inset layout of the main axis rect. So this is how we access it to change legend placement:
customPlot.axisRect()->insetLayout()->setInsetAlignment(0, Qt::AlignTop|Qt::AlignRight);
//Adding new graph for displaying, all on the same plot.
customPlot.addGraph(customPlot.yAxis, customPlot.xAxis);
customPlot.graph(0)->setPen(QPen(QColor(255, 100, 0)));
customPlot.graph(0)->setName("Azimuth function");
//Adding new graph for displaying, all on the same plot.
customPlot.addGraph();
customPlot.graph(1)->setPen(QPen(Qt::red));
customPlot.graph(1)->setName("Elevation function");
//Adding new graph for displaying, all on the same plot.
customPlot.addGraph();
customPlot.graph(2)->setPen(QPen(Qt::green));
customPlot.graph(2)->setName("Z-axis function");
for(int i = 0; i < az.size(); ++i){
azFunct[i] = sin(az[i]*M_PI/180);
elFunct[i] = cos(el[i]*M_PI/180);
}
double minZ = *std::min_element(zFunct.constBegin(), zFunct.constEnd());
double maxZ = *std::max_element(zFunct.constBegin(), zFunct.constEnd());
// pass data points to graphs:
customPlot.graph(0)->setData(xAxisValue, azFunct);
customPlot.graph(1)->setData(xAxisValue, elFunct);
customPlot.graph(2)->setData(xAxisValue,zFunct);
customPlot.xAxis->setVisible(true);
customPlot.xAxis->setTickLabels(false);
customPlot.xAxis2->setVisible(true);
customPlot.xAxis2->setTickLabels(false);
customPlot.yAxis2->setVisible(true);
// set ranges appropriate to show data:
customPlot.yAxis->setRange(-1, 1);
customPlot.yAxis2->setRange(minZ, maxZ);
customPlot.plotLayout()->insertRow(0);
//customPlot.plotLayout()->addElement(0, 0, new QCPTextElement(customPlot, "Z axis", QFont("sans", 12, QFont::Bold)));
// set labels:
customPlot.yAxis->setLabel("sin/cos value");
customPlot.yAxis2->setLabel("Z - axis");
//Under big question have to be tested!
customPlot.replot();
customPlot.savePng(QString(image_path),1500,500,1);}