我需要在测试程序期间创建一系列绘图。我想为此目的使用QCustomPlot
,但是我有一个问题。为了在文件中创建和保存情节,我必须创建QApplication
,在需要从QMainWindow
重新实现我的类之后,然后,我可以在函数中使用QCustomPlot
创建所有需要的图并将其保存到文件中。是否可以避免创建QApplication
实例或QMainWindow
实例,或者同时创建它们两者?
请参见下面的函数代码:
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);}
答案 0 :(得分:0)
当然,您不需要QMainWindow
,可以单独实例化QCustomPlot
实例,而不能作为任何其他子部件的实例。此外,您无需显示该小部件即可进行绘制。因此,即使实例化QApplication
,测试也可以是非交互式的。
当然,QCustomPlot
的设计已损坏;不需要是QWidget
。只有特定的视图视图才需要是小部件。但这是另一个故事。