qml + Qt.createcomponent + visual studio , component is not ready
-
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.1import "./Printer" as Printers
ApplicationWindow {
id:root
objectName: "objWindow1"
visible: true
width: 600
height: 400
title: qsTr("Hello World")
//always on top
// flags: Qt.WindowStaysOnTopHintproperty 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() }}
-
@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".
-
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> -
It is path you are placing in resource. It is like a directory only. So you have to specify the path.
-
ok thanks very much