How to interact with .qml Files without ApplicationWindows (c++ and StackView connection)?



  • Hello,
    I'm working on my firs QtQuick project and I already finished the qml-files and most of the c++ code. I have 6 qml files. The main.qml includes an Application window, a tool bar and the stack view initialisation. The other five .qml files are just items (without ApplicationWindow) that get pushed and poped with stack view.
    I am working with this guide: Interacting with QML Objects from C++ which helped me to do everything I want to in the main.qml. But i can't connect signals or read properties from the other five .qml files. This is the code i use to load the components:

    QApplication app (argc, argv);
    QQmlEngine enigne;
    QQmlComponent component(&engine, QUrl(QStringLiteral("qrl:/main.qml")));
    QObject *object = componet.create();
    QQmlComponent newcomponent(&engine, QUrl(QStringLiteral("qrl:/secondframe.qml")));
    QObject *newobject = newcomponet.create();
    

    I know that it doesn't work because the other .qml files dont have ApplicationWindows because if I add one to the existing file it works. So how do I load the other .qml Files as components into my .cpp code?

    I hope I was able to make myself clear, otherwise feel free to ask any questions.


  • Moderators

    @MilloMille Is it possible for you to load other QML files from main.qml ? What I mean is to load only a single file from C++ and then this QML loads other and so on ? Also can you post some more code ?



  • @p3c0 said in How to interact with .qml Files without ApplicationWindows (c++ and StackView connection)?:

    @MilloMille Is it possible for you to load other QML files from main.qml ? What I mean is to load only a single file from C++ and then this QML loads other and so on ? Also can you post some more code ?

    Yeah sure. Again I reduced the code to the very basics:

    • main.qml
    ApplicationWindow {
              Rectangle{
                        id: homeButton
                        objectName: "homeButton"
                        signal qmlSignal(string msg)
                        MouseArea {
                             onClicked:  {stack.push({item:"qrc:/home.qml}); homeButton.qmlSignal("Hello")}
                        }
              }
              StackView{
                   initalItem: "qrc:/home.qml"
              }
    }
    
    • secondframe.qml
    Item {
              Rectangle{
                        id: test
                        objectName: "test"
                        signal qmlSignal(string msg)
                        MouseArea {
                             onClicked:  {stack.push({item:"qrc:/thirdframe.qml}); test.qmlSignal("Hello")}
                        }
              }
    }
    
    • main.cpp
    QApplication app (argc, argv);
    QQmlEngine enigne;
    QQmlComponent component(&engine, QUrl(QStringLiteral("qrl:/main.qml")));
    QObject *object = componet.create();
    QQmlComponent newcomponent(&engine, QUrl(QStringLiteral("qrl:/secondframe.qml")));
    QObject *newobject = newcomponet.create();
    
    MyClass myClass
    QObject *home = object->findChild<QObject*>("homeButton");
    QObject::connect(home,SIGNAL(qmlSignal(Qstring)), &myClass, SLOT(cppSlot(QString)));
    QObject *test = newobject->findChild<QObject*>("test");
    QObject::connect(test,SIGNAL(qmlSignal(Qstring)), &myClass, SLOT(cppSlot(QString)));
    

    So the cpp Code I wrote is identical for both qml signals It works for the main but not for secondframe. The qml Files are almost identical. The only real difference is that main.qml has an ApplicationWindow, and secondframe.qml starts with an Item. I cant start secondframe with a window, because I dont want to open a new window when i push secondframe.

    Also you might have noticed that I always have to enter the URL when I want to push a new file. I dont know if that is good practise but it works fine for me. The App I designed is similar to a setup wizard, where you click yourself from window to window.

    EDIT: I can also acces Objects in the Inital StackView file as child of main.qml. But I am unable to access the qml files after the intial item. the Loader doesnt help and also messes up the animation because you can see the deeper windows between the animations. Is there an option to give StackView a List of all files that are beeing used so he loads them, but only makes them visible when I push them?



  • Actually i got a very good answer on stackoverflow. Here is the link for everybody that is stuggeling with the same problem
    StackOverflow


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.