库需要 QApplication.如何在Qt Quick项目中使用QApplication
Library requires QApplication. How to use QApplication in Qt Quick project?
我有一个Qt Quick项目,我刚刚添加了一些源文件。尝试构建时,我收到错误消息:
QWidget: Cannot create a QWidget without QApplication
由于我有一个Qt Quick项目,所以我使用QGui应用程序。QApplication是QGuiApplication的一个子类。如何使 QApplication 可用于新添加的源?或者当一个人有一个Qt Quick和一个QWidget时,如何解决它?
源文件是显示图形的QCustomPplot库。
编辑:
主.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.
关键概念是 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()
将Qt Widgets和QML与QWidget::createWindowContainer()相结合
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 无法在 CLion 中构建 C++ 项目
- 运行同一解决方案的另一个项目的项目
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何在选项卡视图Qt中设置一个新项目,并保存以前的项目
- 欧拉项目#8答案是大以获得有效答案
- 从链接列表c++中删除一个项目
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 既然存在危险,为什么项目要使用-I include开关
- cmake在我的项目中所需的所有静态库都不成功
- QT通过C++添加映射QML项目
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 在linux上调试巨大的C++项目
- 在其他文件中创建类时在 c++ 项目中不起作用
- 使外部项目可用于find_package CMake
- 在子目录中使用target_sources()命令时用于单元测试(qtest)的项目结构
- 使用外部SDK工具链文件在VisualStudio上生成项目编译错误
- 如何维护资源管理器项目视图中当前可见的项目列表
- 错误-我无法在VS2019中打开新的Qt项目
- 库需要 QApplication.如何在Qt Quick项目中使用QApplication