gpt4 book ai didi

c++ - 库需要 QApplication。如何在Qt Quick项目中使用QApplication?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:34:59 24 4
gpt4 key购买 nike

我有一个 Qt Quick 项目,我刚刚添加了一些源文件。尝试构建时我收到错误消息:

QWidget: Cannot create a QWidget without QApplication

因为我有一个 Qt Quick 项目,所以我使用 QGuiApplication。 QApplication 是 QGuiApplication 的子类。如何使 QApplication 可用于新添加的源?或者有 Qt Quick 和 QWidget 如何解决?

源文件是显示图形的 QCustomPlot 库。

编辑:

ma​​in.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"));
...
}

您可以在以下帖子中找到详细信息:

Introducing QWidget::createWindowContainer()

Combining Qt Widgets and QML with QWidget::createWindowContainer()

关于c++ - 库需要 QApplication。如何在Qt Quick项目中使用QApplication?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21001243/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com