У меня есть проект Qt Quick, и я просто добавил некоторые исходные файлы. При попытке построить я получаю сообщение об ошибке:Библиотека требует QApplication. Как использовать QApplication в проекте Qt Quick?
QWidget: Cannot create a QWidget without QApplication
Поскольку у меня есть Быстрый проект Qt, я использую QGuiApplication. QApplication является подклассом QGuiApplication. Как сделать QApplication доступным для недавно добавленных источников? Или как решить его, когда у вас есть Qt Quick и QWidget?
Исходными файлами являются библиотека QCustomPlot, которая показывает график.
EDIT:
main.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.
Вы должны создать экземпляр QApplication перед любыми QWidgets созданы. – drescherjm
@drescherjm: Могу ли я использовать циклы QApplication и QGuiApplication в main()? – Phat
№ Что я имел в виду, это создать экземпляр QGuiApplication перед любыми QWidgets. – drescherjm