FileDialog Error: Cannot assign [undefined] to QUrl
-
wrote on 21 Feb 2022, 15:52 last edited by qcoderpro
Hi all,
I used a screenshot rather than coding to show that Qt Creator (Qt 6.2.2) doesn't determine
FileDialog
as a built-in element!The program works well, but when I select the image, it doesn't load into the program and Qt Creator gives this eeror: Error: Cannot assign [undefined] to QUrl
Any idea about the reason, please? -
Hi all,
I used a screenshot rather than coding to show that Qt Creator (Qt 6.2.2) doesn't determine
FileDialog
as a built-in element!The program works well, but when I select the image, it doesn't load into the program and Qt Creator gives this eeror: Error: Cannot assign [undefined] to QUrl
Any idea about the reason, please?wrote on 21 Feb 2022, 16:18 last edited by@qcoderpro said in FileDialog Error: Cannot assign [undefined] to QUrl:
Any idea about the reason, please?
I cannot see
import QtQuick.Dialogs
in your QML
And AFAIK, there is no propertyfileUrl
, perhaps you meanselectedFile
orcurrentFile
==> https://doc.qt.io/qt-6/qml-qtquick-dialogs-filedialog.html
-
@qcoderpro said in FileDialog Error: Cannot assign [undefined] to QUrl:
Any idea about the reason, please?
I cannot see
import QtQuick.Dialogs
in your QML
And AFAIK, there is no propertyfileUrl
, perhaps you meanselectedFile
orcurrentFile
==> https://doc.qt.io/qt-6/qml-qtquick-dialogs-filedialog.html
wrote on 21 Feb 2022, 16:27 last edited by qcoderproI cannot see import QtQuick.Dialogs in your QML
I'm reading this example. Scroll up a littler, please. There, such an import isn't mentioned that's why I didn't add it either.
-
I cannot see import QtQuick.Dialogs in your QML
I'm reading this example. Scroll up a littler, please. There, such an import isn't mentioned that's why I didn't add it either.
wrote on 21 Feb 2022, 16:35 last edited by@qcoderpro said in FileDialog Error: Cannot assign [undefined] to QUrl:
main.qml:36:9: Cannot assign to non-existent property "folder"
Because there is no property
folder
inFileDialog
, I can only see a property calledcurrentFolder
.
I think the example you are using was build with an older version of Qt. -
@qcoderpro said in FileDialog Error: Cannot assign [undefined] to QUrl:
main.qml:36:9: Cannot assign to non-existent property "folder"
Because there is no property
folder
inFileDialog
, I can only see a property calledcurrentFolder
.
I think the example you are using was build with an older version of Qt.wrote on 21 Feb 2022, 16:40 last edited by qcoderproThe issue with
FileDialog
is worked out. Please have a look at code's current situation:import QtQuick import QtQuick.Controls import Qt.labs.platform ApplicationWindow { width: 640 height: 480 visible: true title: qsTr("Hello World") background: Rectangle { color: "darkGray" } Image { id: image anchors.fill: parent fillMode: Image.PreserveAspectFit asynchronous: true } header: ToolBar { Flow { anchors.fill: parent ToolButton { text: qsTr("Open") icon.name: "document-open" onClicked: fileOpenDialog.open() } } } FileDialog { id: fileOpenDialog title: "Select an image file" folder: StandardPaths.writableLocation(StandardPaths.DocumentsLocation) nameFilters: [ "Image files (*.png *.jpeg *.jpg)", ] onAccepted: { image.source = fileOpenDialog.currentFile //selectedFile //fileUrl } } menuBar: MenuBar { Menu { title: qsTr("&File") MenuItem { text: qsTr("&Open...") icon.name: "document-open" onTriggered: fileOpenDialog.open() } } Menu { title: qsTr("&Help") MenuItem { text: qsTr("&About...") // onTriggered: aboutDialog.open() } } } }
For
menuBar: MenuBar
this error raises:
main.qml:45:14: Cannot assign object of type "MenuBar" to property of type "QQuickItem" as the former is neither the same as the latter nor a sub-class of it.* -
The issue with
FileDialog
is worked out. Please have a look at code's current situation:import QtQuick import QtQuick.Controls import Qt.labs.platform ApplicationWindow { width: 640 height: 480 visible: true title: qsTr("Hello World") background: Rectangle { color: "darkGray" } Image { id: image anchors.fill: parent fillMode: Image.PreserveAspectFit asynchronous: true } header: ToolBar { Flow { anchors.fill: parent ToolButton { text: qsTr("Open") icon.name: "document-open" onClicked: fileOpenDialog.open() } } } FileDialog { id: fileOpenDialog title: "Select an image file" folder: StandardPaths.writableLocation(StandardPaths.DocumentsLocation) nameFilters: [ "Image files (*.png *.jpeg *.jpg)", ] onAccepted: { image.source = fileOpenDialog.currentFile //selectedFile //fileUrl } } menuBar: MenuBar { Menu { title: qsTr("&File") MenuItem { text: qsTr("&Open...") icon.name: "document-open" onTriggered: fileOpenDialog.open() } } Menu { title: qsTr("&Help") MenuItem { text: qsTr("&About...") // onTriggered: aboutDialog.open() } } } }
For
menuBar: MenuBar
this error raises:
main.qml:45:14: Cannot assign object of type "MenuBar" to property of type "QQuickItem" as the former is neither the same as the latter nor a sub-class of it.*wrote on 21 Feb 2022, 17:22 last edited by KroMignon@qcoderpro said in FileDialog Error: Cannot assign [undefined] to QUrl:
For menuBar: MenuBar this error raises:
main.qml:45:14: Cannot assign object of type "MenuBar" to property of type "QQuickItem" as the former is neither the same as the latter nor a sub-class of it.*Please take time to read Qt documentation.
InMenuBar
documentation, (https://doc.qt.io/qt-6/qml-qtquick-controls2-menubar.html), you will see there is noMenuItem
used.
I think your code example is outdated, you should be able to update yourself the code to match with documentation. -
@qcoderpro said in FileDialog Error: Cannot assign [undefined] to QUrl:
For menuBar: MenuBar this error raises:
main.qml:45:14: Cannot assign object of type "MenuBar" to property of type "QQuickItem" as the former is neither the same as the latter nor a sub-class of it.*Please take time to read Qt documentation.
InMenuBar
documentation, (https://doc.qt.io/qt-6/qml-qtquick-controls2-menubar.html), you will see there is noMenuItem
used.
I think your code example is outdated, you should be able to update yourself the code to match with documentation.wrote on 21 Feb 2022, 18:43 last edited byI have already seen that page of Docs, but since the example page is itself of Docs too, therefore I thought there might be a problem with my IDE, toolchain or something that such a error turns up.
-
I have already seen that page of Docs, but since the example page is itself of Docs too, therefore I thought there might be a problem with my IDE, toolchain or something that such a error turns up.
wrote on 22 Feb 2022, 06:56 last edited by@qcoderpro said in FileDialog Error: Cannot assign [undefined] to QUrl:
I have already seen that page of Docs, but since the example page is itself of Docs too, therefore I thought there might be a problem with my IDE, toolchain or something that such a error turns up.
They are not the same:
- your example it based on Qt QML Book (AKA Qt Cadaques). This was an Qt external project to help to start with QML. The lead author has left the project and it is not really up to date
- the Qt Doc links I give you are the actual documentation for the current Qt release. There may also be some errors in code parts, but the class/objects definition are always up to date. So you have to adapt the code, but that's all.
-
@qcoderpro said in FileDialog Error: Cannot assign [undefined] to QUrl:
For menuBar: MenuBar this error raises:
main.qml:45:14: Cannot assign object of type "MenuBar" to property of type "QQuickItem" as the former is neither the same as the latter nor a sub-class of it.*Please take time to read Qt documentation.
InMenuBar
documentation, (https://doc.qt.io/qt-6/qml-qtquick-controls2-menubar.html), you will see there is noMenuItem
used.
I think your code example is outdated, you should be able to update yourself the code to match with documentation.wrote on 15 Dec 2023, 10:33 last edited by Bjorn92@KroMignon Just leave
menuBar
out and write:ApplicationWindow { // ... MenuBar { Menu { title: qsTr('&File') MenuItem { text: qsTr('&Open...') icon.name: 'document-open' onTriggered: fileOpenDialog.open() } } Menu { title: qsTr('&Help') MenuItem { text: qsTr('&About') onTriggered: aboutDialog.open() } } } }