gpt4 book ai didi

c++ - 是否可以在单元测试中使用 QCustomPlot?

转载 作者:太空宇宙 更新时间:2023-11-04 12:46:20 26 4
gpt4 key购买 nike

我需要在程序测试期间创建一系列绘图。我想为此目的使用 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);}

最佳答案

当然你不需要QMainWindow,你可以自己实例化一个QCustomPlot实例,而不是作为其他任何东西的子部件。此外,您无需显示该小部件即可绘制它。因此测试可以是非交互式的,即使它们实例化了 QApplication

当然,QCustomPlot的设计是坏的;没有必要让它成为 QWidget。只有绘图的特定 View 需要成为小部件。但那是另一回事了。

关于c++ - 是否可以在单元测试中使用 QCustomPlot?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51336336/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com