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. qml + Qt.createcomponent + visual studio , component is not ready
Forum Updated to NodeBB v4.3 + New Features

qml + Qt.createcomponent + visual studio , component is not ready

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 2 Posters 815 Views
  • 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.
  • Q Offline
    Q Offline
    QtBeginnnnner
    wrote on last edited by
    #1

    I could use Qt.createcomponent("PrinterDialog.qml") with qmlscene main.qml,and a new dialog is showed .
    but while I generated a visual studio project , createcomponents always failed with "component not ready", even if I have copied PrinterDialog..qml to all the folders.

    here is the code
    main.cpp
    int main(int argc, char *argv[]) {
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;

    engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
    if (engine.rootObjects().isEmpty()) return -1;
    
    return app.exec();
    

    }

    main.qml
    import QtQuick 2.9
    import QtQuick.Window 2.2
    import QtQuick.Layouts 1.2
    import QtQuick.Controls 2.2
    import QtQuick.Controls 1.4 as QQC1
    import QtQuick.Controls.Styles 1.4
    import QtQuick.Dialogs 1.1

    import "./Printer" as Printers

    ApplicationWindow {
    id:root
    objectName: "objWindow1"
    visible: true
    width: 600
    height: 400
    title: qsTr("Hello World")
    //always on top
    // flags: Qt.WindowStaysOnTopHint

    property variant win;
    

    // Printers.PrinterDialog{}
    header : ToolBar {
    RowLayout {
    height:idToolButton1.height
    width:idToolButton1.width + idToolButton2.width
    ToolButton {
    id:idToolButton1
    text: qsTr("File")
    onClicked: {
    console.log("File menu click1");
    menu1.open();
    }
    Menu {
    id: menu1
    title: "File"
    topMargin: 30
    leftMargin: 10
    MenuItem {
    text: qsTr("Quit")
    onTriggered: {
    console.log("File menu click2");
    Qt.quit();
    }
    }
    }
    }
    ToolButton {
    id:idToolButton2
    text: qsTr("?")
    onClicked: menu2.open()
    Menu {
    id: menu2
    title: "?"
    topMargin: 30
    leftMargin: 10
    MenuItem {
    text: qsTr("Info")
    onTriggered: {
    console.log("File menu click2");
    console.log("File menu click3");
    messageDialog.open()
    }

    					MessageDialog {
    					id: messageDialog
    					title: "Posconfig"
    					text: "<b>Posconfig</b><br><br>Version 1.24<br>Copy right 2008~2018"
    					Component.onCompleted: visible = false
    					}
                    }
                }
    		}
    	}
    }
    
    Rectangle {
        color:"#112233"
        anchors.fill : parent
    	
        RowLayout{
            anchors.centerIn: parent
            spacing: 100
            QQC1.Button{
    

    /*
    onClicked:{
    var component = Qt.createComponent("Dialog.qml");
    if( component.status == Component.Ready )
    {
    win = component.createObject(root);
    win.show();
    }
    else
    {
    console.log("component not ready1");
    }
    }
    */
    MouseArea {
    anchors.fill: parent
    onClicked:{

    /**/
    var component = Qt.createComponent("./Printer/PrinterDialog.qml");
    if( component.status == Component.Ready )
    {
    win = component.createObject(root);
    win.show();
    }
    else
    {
    console.log("component not ready2");
    }

    		    }
    		   }
               style: ButtonStyle {
                   label: Image {
                       source: "./images/kr_sl_bt_subtotal_print.png"
                       fillMode: Image.PreserveAspectFit  // ensure it fits
                   }
               }
            }
            QQC1.Button{
               style: ButtonStyle {
                   label: Image {
                       source: "./images/kr_sl_bt_subtotal_print.png"
                       fillMode: Image.PreserveAspectFit  // ensure it fits
                   }
               }
            }
            QQC1.Button{
               style: ButtonStyle {
                   label: Image {
                       source: "./images/kr_sl_bt_loadcell.png"
                       fillMode: Image.PreserveAspectFit  // ensure it fits
                   }
               }
            }
            QQC1.Button{
               style: ButtonStyle {
                   label: Image {
                       source: "./images/kr_sl_bt_WES.png"
                       fillMode: Image.PreserveAspectFit  // ensure it fits
                   }
               }
            }
            QQC1.Button{
               style: ButtonStyle {
                   label: Image {
                       source: "./images/kr_sl_bt_info_i.png"
                       fillMode: Image.PreserveAspectFit  // ensure it fits
                   }
               }
            }
        }
    }
    
    footer : Button{
        id:idButton1
        text: qsTr("Close")
        anchors.centerIn: parent.Center
        onClicked:Qt.quit()
    }
    

    }

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      @QtBeginnnnner said in qml + Qt.createcomponent + visual studio , component is not ready:

      ./Printer/PrinterDialog.qml

      Clearly it is path issue. Did you add this in resource file ? What is the path in resource file ? Just try to specify the path like this ":/Printer/PrinterDialog.qml".

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        QtBeginnnnner
        wrote on last edited by QtBeginnnnner
        #3

        Although I don't know the mechanism and logic it is .
        I only adjust path of qrc and path of createComponent to be same , and then it works .
        as my opinion , once qml is successfully loaded with qrc file , I could use it directly , like"Qt.createComponent("PrinterDialog.qml");"
        Why I have to still load qml file with the path Qt.createComponent("./Printer/PrinterDialog.qml");
        so weird...................................

        worked modification as follows:
        in qml file:
        var component = Qt.createComponent("./Printer/PrinterDialog.qml");
        if( component.status == Component.Ready )
        {
        win = component.createObject(root);
        win.show();
        }
        else
        {
        console.log("component not ready2");
        }

        in qrc file:
        <RCC>
        <qresource prefix="/">
        <file>main.qml</file>
        <file>./Printer/PrinterDialog.qml</file>
        </qresource>
        </RCC>

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          It is path you are placing in resource. It is like a directory only. So you have to specify the path.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          1
          • Q Offline
            Q Offline
            QtBeginnnnner
            wrote on last edited by
            #5

            ok thanks very much

            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