2

I use Qt 5.5.0 and Qt Creator 3.4.2 on Mac-book, I want to open second qml on button click, I want to develop application in which login page come and after login control move to next page.

I try suggestion of stackoverflow, but still I face problem to open next qml on button click.

I tried

button2.onClicked:{
                        var component = Qt.createComponent("test.qml")
                        var window    = component.createObject(root)
                        window.show()
        }

How can I create a new window from within QML?

In upper case error come related to root, ReferenceError: root is not defined. I gave root id in test.qml file.

I also tried many more example to solve to develop functionality. Please give brief idea or code to develop this functionality.

Community
  • 1
  • 1
Tejas Virpariya
  • 224
  • 1
  • 6
  • 15

2 Answers2

3

The code below works, Take note of the property status and the function errorString() – they will give you information in debugging (.e.g. "file not found" errors, components not ready, etc).

See these pages for background:
Dynamic QML Object Creation from JavaScript
Component QML Type
Window QML Type

   function createWindows() {
     var component = Qt.createComponent("Child.qml");
     console.log("Component Status:", component.status, component.errorString());
     var window = component.createObject(root, {"x": 100, "y": 300});
     window.show();
    }
ChrisF
  • 134,786
  • 31
  • 255
  • 325
spring
  • 18,009
  • 15
  • 80
  • 160
  • I have just started learning QT. Can you help to understand what is `root` in your above code snippet ? – Vikrant Jun 28 '18 at 09:19
  • @Vikrant – see the `Component` documentation for `createObject` http://doc.qt.io/qt-5/qml-qtqml-component.html#createObject-method – "root" is just a variable name for an optional parent object – spring Jun 28 '18 at 17:45
0

For this type of ui I'd use [StackView][1] component that allows also animations between transitions. Other solution is to use a Loader component.

sk2212
  • 1,688
  • 4
  • 23
  • 43
Cristea Bogdan
  • 116
  • 1
  • 11