0

Why qmlEngine.load(url) dont work in init function on RELEASE? I have function for init class instead default constructor Why is isnt work?

#include <QApplication>
#include <QQmlApplicationEngine>


class window : public QApplication
{
public:
    explicit window(int argc, char *argv[]);
    void init();
private:
     QQmlApplicationEngine engine;
};
void window::init()
{
     const QUrl url(QStringLiteral("qrc:/main.qml"));
     QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
                      this, [url](QObject *obj, const QUrl &objUrl) {
          if (!obj && url == objUrl)
               QCoreApplication::exit(-1);
     }, Qt::QueuedConnection);
     engine.load(url);
}



int main(int argc, char *argv[])
{
     window w(argc,argv);
     w.init();
     return w.exec();
}

  • I posted an answer, but, because @Burak wasn't happy with it and was downvoted trigger-happy, I have deleted it. However, in the vain of helping you out, I still STRONGLY believe the problem is you are not constructing correctly hence that is why you're getting inconsistent behavior because the class isn't properly constructed. – Stephen Quan Oct 25 '22 at 22:42
  • Just because @Burak didn't understand the answer. And the nature of the rhetoric question was intended to direct the OP to ask the question about the constructor and I DID provide the correct constructor was meant to lead to the correct solution. It is Burak who isn't invested in either the question or the answer, but, felt it was opportunistic to downvote the experience on StackOverflow, in this instant, completely distasteful. – Stephen Quan Oct 25 '22 at 22:47
  • 1
    How did you check that isn't work? And don't you need `QGuiApplication` instead? `QApplication` is intended for `QWidget` classes as I [understand it](https://doc.qt.io/qt-6/qguiapplication.html#details). Actually I don't understand this approach to use a class instead of doing the same in the main(). – folibis Oct 26 '22 at 09:47

0 Answers0