Domanda

Ho un progetto Qt Quick e ho appena aggiunto alcuni file di origine.Quando si tenta di costruire ottengo il messaggio di errore:

QWidget: Cannot create a QWidget without QApplication
.

Dal momento che ho un progetto Qt Quick, uso la qgùapplicazione.QApplication è una sottoclasse di qguiapplicazione.Come faccio a rendere QApplication disponibile per le fonti appena aggiunte?O come si risolve uno quando si ha un QT Quick and A Qwidget?

I file di origine sono la libreria QCustomPlot che mostra un grafico.

Modifica:

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();
}
.

Errore:

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

È stato utile?

Soluzione

Il concetto chiave è QWidget :: createwindawContainer () .Prova il seguente codice:

#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"));
    ...
 }
.

Puoi trovare i dettagli sui seguenti messaggi:

Presentazione QWidget :: createwindowContainer ()

Combinando i widget Qt e QML con QWidget :: CreatewindowContainer ()

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top