我有一个 Qt Quick 项目,我刚刚添加了一些源文件。尝试构建时,我收到错误消息:
QWidget: Cannot create a QWidget without QApplication
因为我有一个 Qt Quick 项目,所以我使用 QGuiApplication。QApplication 是 QGuiApplication 的子类。如何使 QApplication 可用于新添加的源?或者当一个人拥有 Qt Quick 和 QWidget 时如何解决它?
源文件是显示图形的 QCustomPlot 库。
编辑:
主.cpp:
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QtQuick2ApplicationViewer viewer;
//Register C++ classes with QML
qmlRegisterType<Bluetooth>("Bluetooth", 1, 0, "Bluetooth");
//Set start QML file
viewer.setMainQmlFile(QStringLiteral("qml/test/main.qml"));
//New Code:
// generate some data:
QWidget widget;
QCustomPlot * customPlot = new QCustomPlot(&widget);
QVector<double> x(101), y(101); // initialize with entries 0..100
for (int i=0; i<101; ++i)
{
x[i] = i/50.0 - 1; // x goes from -1 to 1
y[i] = x[i]*x[i]; // let's plot a quadratic function
}
// create graph and assign data to it:
customPlot->addGraph();
customPlot->graph(0)->setData(x, y);
// give the axes some labels:
customPlot->xAxis->setLabel("x");
customPlot->yAxis->setLabel("y");
// set axes ranges, so we see all data:
customPlot->xAxis->setRange(-1, 1);
customPlot->yAxis->setRange(0, 1);
customPlot->replot();
//New Code End
//Show GUI
viewer.showExpanded();
return app.exec();
}
错误:
QML debugging is enabled. Only use this in a safe environment.
QWidget: Cannot create a QWidget without QApplication
Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.