2014-01-08 2 views
5

Qt Quick 프로젝트가 있고 방금 일부 소스 파일을 추가했습니다. 빌드를 시도 할 때 다음과 같은 오류 메시지가 나타납니다.라이브러리에는 QApplication이 필요합니다. Qt Quick 프로젝트에서 QApplication을 사용하는 방법은 무엇입니까?

QWidget: Cannot create a QWidget without QApplication 

Qt Quick 프로젝트가 있으므로 QGuiApplication을 사용합니다. QApplication은 QGuiApplication의 하위 클래스입니다. 새로 추가 된 소스에서 QApplication을 사용하려면 어떻게합니까? 또는 Qt Quick와 QWidget을 가지고있을 때 어떻게 해결할 수 있습니까?

원본 파일은 그래프를 표시하는 QCustomPlot 라이브러리입니다.

편집 :

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

당신은 어떤 QWidgets가 생성되기 전에의 QApplication 인스턴스를 생성해야합니다. – drescherjm

+0

@drescherjm : QApplication과 QGuiApplication 루프를 모두 main()에 둘 수 있습니까? – Phat

+0

아니요. QWidgets보다 먼저 QGuiApplication 인스턴스를 작성해야한다는 의미였습니다. – drescherjm

답변

4

의 핵심 개념은 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()

+0

감사합니다. 이것은 매우 유망한 것으로 보입니다. 나는 아직 그것을 테스트하지 않았지만 이것은 내가 찾고있는 것이었다. 또한 제공된 링크 중 하나에서 Android에서 작동하지 않는다고 읽었습니다. 내 질문에 이것을 지정하지 않았지만 Android는 내가 개발할 플랫폼 중 하나입니다. Android에서 하나의 OpenGL 표면에만 제한되어 있기 때문에 (적어도 Qt 5.1에서는 Qt 5.2에서 수정되었는지 알 수 없기 때문에) 읽을 수는 없습니다. 해결 방법에 대한 제안 사항이 있으시면 자유롭게 의견을 말하십시오. 나는 그것이 어쨌든 작동하는지 시험해 볼 것입니다. (손가락과 발가락을 횡단합니다) – Phat

관련 문제