C++ 库需要QApplication。如何在Qt-Quick项目中使用QApplication?
我有一个QtQuick项目,我刚刚添加了一些源文件。尝试生成时,我收到错误消息:C++ 库需要QApplication。如何在Qt-Quick项目中使用QApplication?,c++,qt,qtgui,qtquick2,qcustomplot,C++,Qt,Qtgui,Qtquick2,Qcustomplot,我有一个QtQuick项目,我刚刚添加了一些源文件。尝试生成时,我收到错误消息: QWidget: Cannot create a QWidget without QApplication 因为我有一个QtQuick项目,所以我使用Qgui应用程序。QApplication是QGUI应用程序的一个子类。如何使QApplication可用于新添加的源?或者,当一个人有一个QtQuick和一个QWidget时,如何解决这个问题 源文件是显示图形的QCustomPlot库 编辑: int main(
QWidget: Cannot create a QWidget without QApplication
因为我有一个QtQuick项目,所以我使用Qgui应用程序。QApplication是QGUI应用程序的一个子类。如何使QApplication可用于新添加的源?或者,当一个人有一个QtQuick和一个QWidget时,如何解决这个问题
源文件是显示图形的QCustomPlot库
编辑:
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.
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.
关键概念是。请尝试以下代码:
#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"));
...
}
#包括
int main(int argc,char*argv[])
{
QApplication应用程序(argc、argv);
QQuickView*视图=新的QQuickView();
QWidget*container=QWidget::createWindowContainer(视图,此);
容器->设置最小尺寸(200200);
容器->设置最大尺寸(200200);
容器->设置焦点策略(Qt::TabFocus);
视图->设置源(QUrl(“qml/test/main.qml”);
...
}
您可以在以下帖子中找到详细信息:
在创建任何QWidget之前,必须先创建QApplication实例。@drescherjm:我可以在main()中同时使用QApplication和QGuiApplication循环吗?不是。我的意思是在任何QWidgets之前创建QGUI应用程序实例。@drescherjm:按照我之前编辑的操作时仍然会出现错误。@Phataas您现在遇到的错误是什么?相同或不同。。你能展示你正在做的更多代码吗?谢谢,这看起来很有希望。我还没有测试过,但这正是我想要的。我还从你提供的一个链接中读到,这在Android上不起作用。我在问题中没有具体说明这一点,但Android是我开发的平台之一。据我所知,这是不可能的,因为在Android上,一个OpenGL曲面仅限于一个(至少在Qt5.1上,不知道Qt5.2中是否修复了它)。如果您对如何解决这一问题有任何建议,请随时发表评论。我会试试看它是否有效。(手指和脚趾交叉)我也有同样的问题。我的解决方案只是使用QApplication而不是QGUI应用程序。一切都很完美!