Библиотека требует qapplication.Как использовать qapplation в Qt Quick Project?

StackOverflow https://stackoverflow.com//questions/21001243

Вопрос

У меня есть Qt быстрый проект, и я просто добавил некоторые исходные файлы.При попытке строить, я получаю сообщение об ошибке:

QWidget: Cannot create a QWidget without QApplication
.

Поскольку у меня есть Qt быстрый проект, я использую QGuiaPlication.Qapplication - это подкласс QGuiacplication.Как мне сделать QAPLICATION доступно для вновь добавленных источников?Или как можно решить, когда у кого-то есть Qt быстрая и qwidget?

Исходные файлы - это библиотека QCustomott, которая показывает график.

Редактировать:

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.
.

Это было полезно?

Решение

Концепция ключей - 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 и QML с Qwidget :: Createwindowcontainer ()

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top