라이브러리에는 qapplication이 필요합니다.QT 빠른 프로젝트에서 qapplication을 사용하는 방법은 무엇입니까?

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

문제

Qt 빠른 프로젝트가 있고 방금 소스 파일을 추가했습니다.빌드하려고 할 때는 오류 메시지가 나타납니다.

QWidget: Cannot create a QWidget without QApplication
.

QT QUITAPPLICATION을 사용하고 있기 때문에 QGUIAPPLICATION을 사용합니다.qapplication은 QGUIApplication의 하위 클래스입니다.새로 추가 된 소스에서 qapplication을 어떻게 사용할 수 있습니까?또는 Qt 빠른 Qt와 qwidget이있을 때 어떻게 해결할 수 있습니까?

소스 파일은 그래프를 보여주는 QCustOplot 라이브러리입니다.

편집 :

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 ()

QWIDGET와 QT 위젯 및 QML 결합 :: CreateWindowContainer ()

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top