Library requires QApplication. How to use QApplication in Qt Quick project?
Asked Answered
A

1

6

I have a Qt Quick project and I just added some source files. When trying to build I get the error message:

QWidget: Cannot create a QWidget without QApplication

Since I have a Qt Quick project I use the QGuiApplication. QApplication is a subclass of QGuiApplication. How do I make QApplication available to the newly added sources? Or how do one solve it when one have a Qt Quick and a QWidget?

The source files are the QCustomPlot library which shows a graph.

EDIT:

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

Error:

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.
Acetanilide answered 8/1, 2014 at 16:30 Comment(7)
You have to create the QApplication instance before any QWidgets are created.Sheri
@drescherjm: Can I have both QApplication and QGuiApplication loops in main()?Acetanilide
No. What I meant was create your QGuiApplication instance before any QWidgets.Sheri
@drescherjm: Still get the error when doing as I have edited in question.Acetanilide
@Phataas Which error are you getting now? Same one or different.. Can you show some more code you are doing..Nicholnichola
@Digital_Reality: Same error. I will post whole main.cpp and error log.Acetanilide
I am able to rename QGuiApplication to QApplication, but I then get two main windows. One based on the QML and one off the widget. Is it possible to integrate these two somehow? Also I don't know if there will be any other side effects.Acetanilide
T
4

The key concept is QWidget::createWindowContainer(). Try the following code:

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

You can find the details on the following posts:

Introducing QWidget::createWindowContainer()

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

Tibold answered 8/1, 2014 at 18:35 Comment(2)
Thanks, this seems very promising. I have not tested it yet, but this was what I was looking for. I also read in one of the links you provided that this does not work on Android. I did not specify this in my question, but Android is one of the platforms I am developing for. From what I read it is not possible because on Android one is limited to only one OpenGL surface (at least on Qt 5.1, don't know if it's fixed in Qt 5.2). If you have any suggestions on how that can be solved please feel free to comment on that. I will try and see if it works anyways.(crossing fingers and toes)Acetanilide
I had the same problem. My solution was simply to use QApplication instead of QGuiApplication. And everything works perfectly!Adalai

© 2022 - 2024 — McMap. All rights reserved.