Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-127443

Snippet update for QMLTC documentation

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Fixed
    • Not Evaluated
    • None
    • 6.7.1
    • None
    • 1c7e8aa0f (dev), 2b1997414 (6.8), 124052699 (6.7)

    Description

      Link: https://doc.qt.io/qt-6/qtqml-qml-type-compiler.html#using-the-generated-c

      In this documentation, we have this snippet.

      Here, it creates QQuickWindow and the documentRoot.
      However, when I tried qmltc with a simple app, where there's only one QML file(Main.qml) like so: (The project is attached btw. This is tested with Qt 6.7.1 msvc desktop)

      import QtQuick
      Window {
          width: 640
          height: 480
          visible: true
          title: qsTr("Hello World")    
          Rectangle {
              anchors.centerIn: parent
              width: 300
              height: 300
              color: "red"
          }
      } 

      The generated main.h has two classes:

      class Main : public QQuickWindowQmlImpl

      and 

      class Main_Rectangle : public QQuickRectangle 

      If I were to follow the snippet in the documentation, I was supposed to instantiate Main_Retangle and set its parent to QQuickWindow, but since Main_Rectangle's constructor was protected, I couldn't do it. Also, instantiating Main and calling setParentItem() of it didn't work either because it doesn't have setParentItem().

      Hense, the snippet ended up like this, where I didn't create QQuickWindow separately and directly instantiate the Main class.

      #include <QGuiApplication>
      #include <QQmlApplicationEngine>
      #include "main.h"
      
      int main(int argc, char *argv[])
      {    
          QGuiApplication app(argc, argv);
          app.setApplicationDisplayName(QStringLiteral("This example is powered by qmltc!"));    
          QQmlEngine e;    
          QScopedPointer<QMLTCTest::Main> window(new QMLTCTest::Main(&e));   
          window->show();
          app.exec();
      }
       

      and it worked:

      Therefore, I think the snippet should be revised like this:

      #include "myapp.h" // include generated C++ header
      
      QGuiApplication app(argc, argv);
      app.setApplicationDisplayName(QStringLiteral("This example is powered by qmltc!"));
      
      QQmlEngine e;
      
      QScopedPointer<QmltcExample::myApp> window(new QmltcExample::myApp(&e));
      
      // ...
      
      window.show();
      app.exec(); 

       

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            lexcaz Alexei Cazacov
            mikio_hirai Mikio Hirai
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes