我有一个Qt快速项目,我只是添加了一些源文件。当我试图构建时,我得到了错误消息:
QWidget: Cannot create a QWidget without QApplication因为我有一个Quick,所以我使用QGuiApplication。QApplication是QGuiApplication的子类。如何将QApplication提供给新添加的源?或者,当一个人拥有一个QtQuick和一个QWidget时,如何解决这个问题?
源文件是显示图形的QCustomPlot库。
编辑:
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.发布于 2014-01-08 18:35:36
关键的概念是QWidget::createWindowContainer容器()。尝试以下代码:
#include <QQuickView>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QQuickView *view = new QQuickView();
QWidget *container = QWidget::createWindowContainer(view, this);
container->setMinimumSize(200, 200);
container->setMaximumSize(200, 200);
container->setFocusPolicy(Qt::TabFocus);
view->setSource(QUrl("qml/test/main.qml"));
...
}您可以找到以下帖子的详细信息:
介绍QWidget::createWindowContainer容器()
将QWidget和QML与QWidget::createWindowContainer容器()相结合
https://stackoverflow.com/questions/21001243
复制相似问题