Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to use qmlpreview?
Forum Updated to NodeBB v4.3 + New Features

How to use qmlpreview?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qmlqmlpreview
13 Posts 4 Posters 415 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • sierdzioS Offline
    sierdzioS Offline
    sierdzio
    Moderators
    wrote last edited by
    #2

    Your QML code is compiled into the executable, so you need to recompile after changing the code.

    You can load QML from disk, without compiling it into the exe, that will require some code changes though.

    (Z(:^

    1 Reply Last reply
    1
    • R Offline
      R Offline
      rudolflovrencic
      wrote last edited by
      #3

      I have also tried to have a CMake option that dictates if files are hot-reloadable or not. Here is a GitHub project where I almost managed to make that work. It is relying on the typical Loader+QFileSystemWatcher approach instead of using the qmlpreview tool - and it does not fully work as expected (see this issue).

      @sierdzio said in How to use qmlpreview?:

      You can load QML from disk, without compiling it into the exe, that will require some code changes though.

      I am fully on board for code changes but I am unable to see which changes are necessary. Where can I learn how to prepare my project in order to use qmlpreview?

      1 Reply Last reply
      0
      • GrecKoG Offline
        GrecKoG Offline
        GrecKo
        Qt Champions 2018
        wrote last edited by
        #4

        I've not really read your QML project but I believe using Loader is not the correct approach since that's different from the production code you want.

        What you want is a way to point your qml code to actual files instead of qrc files and then a way to reload it.

        The first can be done in CMake if you have proper QML modules. Here's a function I wrote doing that:

        function (enable_qml_hotreload target)
            get_target_property(source_dir ${target} SOURCE_DIR)
            get_target_property(qmldir_content ${target} _qt_internal_qmldir_content)
            string(REGEX REPLACE "prefer [^\n]*" "prefer ${source_dir}/" qmldir_content "${qmldir_content}")
            set_property(TARGET ${target} PROPERTY _qt_internal_qmldir_content "${qmldir_content}")
        endfunction()
        

        Do note that this is using a internal variable so it is not guaranteed to work in the future.

        What this does is changing the folder pointer to by the generated qmldir file to your local source files.

        Then from I understanding qmlpreview should handle the reloading itself if you have defined QT_QML_DEBUG. I haven't used this as I prefer to do a reload on demand via a shortcut that then calls clearSingletons, clearComponentCache and loadFromModule.

        R 1 Reply Last reply
        0
        • GrecKoG GrecKo

          I've not really read your QML project but I believe using Loader is not the correct approach since that's different from the production code you want.

          What you want is a way to point your qml code to actual files instead of qrc files and then a way to reload it.

          The first can be done in CMake if you have proper QML modules. Here's a function I wrote doing that:

          function (enable_qml_hotreload target)
              get_target_property(source_dir ${target} SOURCE_DIR)
              get_target_property(qmldir_content ${target} _qt_internal_qmldir_content)
              string(REGEX REPLACE "prefer [^\n]*" "prefer ${source_dir}/" qmldir_content "${qmldir_content}")
              set_property(TARGET ${target} PROPERTY _qt_internal_qmldir_content "${qmldir_content}")
          endfunction()
          

          Do note that this is using a internal variable so it is not guaranteed to work in the future.

          What this does is changing the folder pointer to by the generated qmldir file to your local source files.

          Then from I understanding qmlpreview should handle the reloading itself if you have defined QT_QML_DEBUG. I haven't used this as I prefer to do a reload on demand via a shortcut that then calls clearSingletons, clearComponentCache and loadFromModule.

          R Offline
          R Offline
          rudolflovrencic
          wrote last edited by
          #5

          @GrecKo Thank you for your response. I haven't been able to make it work - I've made a branch cmake-qml-redirect (link) that starts to implement your suggestion, but I'm getting No qmldir at "/path/to/project/root/" at project build time.

          Do you have an example of how that should be set up?

          1 Reply Last reply
          0
          • GrecKoG Offline
            GrecKoG Offline
            GrecKo
            Qt Champions 2018
            wrote last edited by
            #6

            Honestly I don't know, I tried to make it work with qmlpreview yesterday after my message to see if it would be simple but I didn't unsterstand much.

            I recompiled qmlpreview to add more debug messages but I think I need message on the other side (the qml debug server running in the application) to get the full picture.
            The window closed and re-opened itself upon a change but the change was ignored.

            I'll ask Qt commercial support about this. For now the manual method with your own QFileSystemWatcher or shortcut seems to be the sensible one.

            1 Reply Last reply
            1
            • GrecKoG Offline
              GrecKoG Offline
              GrecKo
              Qt Champions 2018
              wrote last edited by
              #7

              As for your cmake / qmldir issue I'm not sure. I first tried it with a simpler CMake like this:

              ...
              qt_add_executable(appuntitled40
                  main.cpp
              )
              
              qt_add_qml_module(appuntitled40
                  URI untitled40
                  VERSION 1.0
                  QML_FILES
                      Main.qml
              )
              ...
              

              I do use it one a more complicated project with multiple CMakeLists.txt and modules though.

              R 1 Reply Last reply
              0
              • GrecKoG GrecKo

                As for your cmake / qmldir issue I'm not sure. I first tried it with a simpler CMake like this:

                ...
                qt_add_executable(appuntitled40
                    main.cpp
                )
                
                qt_add_qml_module(appuntitled40
                    URI untitled40
                    VERSION 1.0
                    QML_FILES
                        Main.qml
                )
                ...
                

                I do use it one a more complicated project with multiple CMakeLists.txt and modules though.

                R Offline
                R Offline
                rudolflovrencic
                wrote last edited by
                #8

                @GrecKo Are you able to share main.cpp for such setup? I am blocked on that.

                GrecKoG 1 Reply Last reply
                0
                • R rudolflovrencic

                  @GrecKo Are you able to share main.cpp for such setup? I am blocked on that.

                  GrecKoG Offline
                  GrecKoG Offline
                  GrecKo
                  Qt Champions 2018
                  wrote last edited by
                  #9

                  @rudolflovrencic just a default generated main.cpp:

                  #include <QApplication>
                  #include <QQmlApplicationEngine>
                  
                  int main(int argc, char *argv[])
                  {
                      QApplication app(argc, argv);
                      QQmlApplicationEngine engine;
                      engine.loadFromModule("untitled40", "Main");
                  
                      return app.exec();
                  }
                  
                  R 1 Reply Last reply
                  0
                  • GrecKoG GrecKo

                    @rudolflovrencic just a default generated main.cpp:

                    #include <QApplication>
                    #include <QQmlApplicationEngine>
                    
                    int main(int argc, char *argv[])
                    {
                        QApplication app(argc, argv);
                        QQmlApplicationEngine engine;
                        engine.loadFromModule("untitled40", "Main");
                    
                        return app.exec();
                    }
                    
                    R Offline
                    R Offline
                    rudolflovrencic
                    wrote last edited by
                    #10

                    @GrecKo Don't you handle key press signals from QML and invoke clearSingletons, clearComponentCache and loadFromModule in main.cpp? That was the impression I got from post #4 where you introduced the enable_qml_hotreload CMake function.

                    Basically, I'm asking: how do you currently do a hot reload with a key press? Minimal working example or a bit more details would be useful since I'm a bit lost in the advice so far. I'm especially interested in your approach since you said that you maintain the same QML in production and for hot reloading:

                    @GrecKo said in How to use qmlpreview?:

                    ...but I believe using Loader is not the correct approach since that's different from the production code you want.

                    In other words, you avoid the QML scaffolding needed for hot reloading (apart from catching the hot reload key press, I guess).

                    1 Reply Last reply
                    0
                    • GrecKoG Offline
                      GrecKoG Offline
                      GrecKo
                      Qt Champions 2018
                      wrote last edited by
                      #11

                      @rudolflovrencic Either use a recursive QFileSystemWatcher or a QML Shortcut / c++ eventFilter depending on you want to trigger the reload, then connect that to a function which does the following:

                      for (QObject* rootObject : m_engine->rootObjects()) {
                        rootObject->deleteLater();
                      }
                      QMetaObject::invokeMethod(this, [this] {
                          m_engine->clearSingletons();
                          m_engine->clearComponentCache();
                          m_engine->loadFromModule("MyAppModuleUri", "Main");
                      }, Qt::QueuedConnection);
                      
                      1 Reply Last reply
                      2
                      • MarkkyboyM Offline
                        MarkkyboyM Offline
                        Markkyboy
                        wrote last edited by
                        #12

                        How are you starting/trying to start QML preview?, I use it when creating desktop apps for Windows 10 and to an extent, for my Android phone.

                        Don't just sit there standing around, pick up a shovel and sweep up!

                        I live by the sea, not in it.

                        R 1 Reply Last reply
                        0
                        • MarkkyboyM Markkyboy

                          How are you starting/trying to start QML preview?, I use it when creating desktop apps for Windows 10 and to an extent, for my Android phone.

                          R Offline
                          R Offline
                          rudolflovrencic
                          wrote last edited by
                          #13

                          @Markkyboy As I mentioned in the original post, just qmlpreview myexe. The executable myexe is compiled with QML debugging enabled.

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved