Qt's FileDialog defaultSuffix not functionning - qt

I'm using the following code to the new property of the filedialog under QtQuick.Dialogs 1.3 & Qt 5.10.0. I've build it using Qt Creator 5.10 default kit.
import QtQuick 2.10
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.3
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
FileDialog {
id: fileDialog
title: "Please choose a file"
folder: shortcuts.home
defaultSuffix: "txt"
selectExisting: false
selectMultiple: false
onAccepted: {
console.log("You chose: " + fileUrl)
Qt.quit()
}
onRejected: {
console.log("Canceled")
Qt.quit()
}
Component.onCompleted: visible = true
}
}
My expectation are that if I choose a filename such as "MyFile", then the fileUrl would be "MyFile.txt". However it just returns "MyFile".

I have tested the code and it comes does come out with the ".txt". I am unsure of the problem but since there isn't really a question, you could append the default suffix using a global property.

Related

QtQuickControls FileDialog Automation

I'm trying to automate picking a file in QML in a QtQuick Controls FileDialog. How can I invoke FileDialog's accept with a specific fileUrl when the fileUrl property is read only?
The current attempt involves calling filedialog.clearSelection, filedialog.addSelection and finally filedialog.accept. clearSelection and addSelection are not documented but can be found in https://github.com/qt/qtquickcontrols/blob/dev/src/dialogs/qquickfiledialog.cpp (Assuming Qt has used a DefaultFileDialog as this can be system dependent)
However clearSelection seems to only work sporadically, having no affect if the same FileDialog has been used manually, hence the addSelection fails to set fileUrl.
The following is a QML file (loaded as a basic project within QtCreator) demonstrates this. with a manual file dialog open button and an automatic one:
import QtQuick 2.9
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.4
import QtQuick.Dialogs 1.3
Window {
visible: true;width: 200;height:200
FileDialog {id: filedialog; onAccepted: console.log("File Dialog Accepted: ", fileUrl, fileUrls);}
Row {
Button {text: "manual"; onClicked: filedialog.open()}
Button {
text: "auto_qml"
onClicked: {
console.log("Current selection:", filedialog.fileUrl, filedialog.fileUrls)
filedialog.clearSelection();
console.log("cleared selection:", filedialog.fileUrl, filedialog.fileUrls) // only clears selection if manual not used
let t = filedialog.addSelection("file:/home/user/tempfile.txt");
console.log("add selection success:", t) // a non existent file returns false, so file must exist
filedialog.accept()
}
}
}
}
As variant you can use Qt.labs.platform 1.1 library. It contains FileDialog with a little bit another behavior - file property is not read-only.
And you can do like so:
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.4
import Qt.labs.platform 1.1
Window {
width: 640
height: 480
visible: true
title: qsTr("Hello World")
FileDialog {id: filedialog; onAccepted: console.log("File Dialog Accepted: ", file, files);}
Row {
Button {text: "manual"; onClicked: filedialog.open()}
Button {
text: "auto_qml"
onClicked: {
filedialog.file = "file:/home/user/tempfile.txt";
console.log("Current selection:", filedialog.file, filedialog.files)
filedialog.accepted()
}
}
}
}

QtQuick button shows outside of dialog

I'm trying to make a customizable single button error message dialog in QtQuick, but when I open the dialog the "Ok" button is shown outside of it:
it looks like this
When I open the dialog again, it works normally, the button is shown at the right place.
Here is a minimal working example :
import QtQuick 2.11
import QtQuick.Window 2.11
import QtQuick.Controls 2.4
Window {
visible: true
width: 640
height: 480
Button {
text: "Open Dialog"
onClicked: {
dialog.show("This text can be customized.")
}
Dialog {
id: dialog
standardButtons: Dialog.Ok
title: "Error"
modal: true
function show(txt) {
label.text = txt
open()
}
Label {
id: label
}
}
}
}
I'm using Qt 5.11.3 with QtQuick 2.11, build target is Desktop GCC 64 bits.
Is this a bug? What am I doing wrong?
It seems to be a bug in Qt 5.11.3 or in my installation of it on Linux.
Here is the workaround I used, basically recreating the Dialog from a regular Popup :
import QtQuick 2.0
import QtQuick.Controls 2.4
import QtQuick.Layouts 1.3
Popup {
x: (parent.width-width)/2
y: (parent.height-height)/2
modal: true
closePolicy: Popup.NoAutoClose
property var title: "Error"
property var msg: ""
function show(message) {
msg = message
open()
}
ColumnLayout {
spacing: 30
Label {
Layout.alignment: Qt.AlignLeft
Layout.preferredHeight: 5
font.bold: true
text: title
}
Label {
text: msg
}
Button {
Layout.alignment: Qt.AlignRight
text: "Ok"
onClicked: {
close()
}
}
}
}

FileDialog shows no files

I have this code in main.qml:
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2
import QtQuick.Controls 2.2
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Test")
FileDialog {
id: fileDialog
folder: shortcuts.home
visible: true
title: "Please choose an image"
nameFilters: ["Image files (*.jpg *.png)", "All files (*)"]
onAccepted: {
image.source = fileDialog.fileUrl
}
}
Image {
id: image
anchors.centerIn: parent
}
}
When I run it by pressing Run in QtCreator, I get a file dialog with no files listed.
Screenshot 1
It lists files if I run it as qmlscene-qt5 main.qml, but this way I have no filters available (All Files only).
Screenshot 2
You have to make it visible when the item has been completely created:
FileDialog {
id: fileDialog
folder: shortcuts.home
// visible: true <--- ---
title: "Please choose an image"
nameFilters: [ "Image files (*.jpg *.png)", "All files (*)" ]
onAccepted: {
image.source = fileDialog.fileUrl
}
Component.onCompleted: visible = true // <--- +++
}

QML FileDialog issue

I am trying to use the FileDialog component in QML.
I have done exactly the same code that is in the Qt documentation at the link http://doc.qt.io/qt-5/qml-qtquick-dialogs-filedialog.html and it does not show the dialog and returns the error: QFileInfo::absolutePath: Constructed with empty filename. I tried to write a simple code to test it and the return was the same error. My code is below.
import QtQuick 2.4
import QtQuick.Window 2.2
import QtQuick.Controls 1.3
import QtQuick.Dialogs 1.2
Window {
visible: true
width: 360
height: 640
maximumHeight: 640
minimumHeight: 640
maximumWidth: 360
minimumWidth: 360
title: "Acessar Galeria Test"
Rectangle {
id: principal
anchors.fill: parent
FileDialog {
id: fileDialog
title: "Please choose a file"
folder: shortcuts.home
visible: true
}
}
}
Answering my own question:
FileDialog visible property cannot be true while the component is not complete. So the code must be like below:
import QtQuick 2.4
import QtQuick.Window 2.2
import QtQuick.Controls 1.3
import QtQuick.Dialogs 1.2
Window {
visible: true
width: 360
height: 640
maximumHeight: 640
minimumHeight: 640
maximumWidth: 360
minimumWidth: 360
title: "Acessar Galeria Test"
Rectangle {
id: principal
anchors.fill: parent
FileDialog {
id: fileDialog
title: "Please choose a file"
folder: shortcuts.home
visible: false
}
}
Component.onCompleted: {
fileDialog.visible = true;
}
}

How to edit App menu from Qt Creator?

I start "QML App with controls" project in Qt Creator. I see that I can add to canvas different kind of controls, but I do not see how I can in graphical mode edit menu like: File, View, Edit... In constructor on canvas it's simple do not exists, but it's exists of running app, like http://img.ctrlv.in/img/15/10/03/560f856edb26c.png
You can create the menu in the main.qml file, here is an example application:
import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Window 2.2
import QtQuick.Dialogs 1.2
ApplicationWindow {
title: qsTr("Hello World")
width: 640
height: 480
visible: true
menuBar: MenuBar {
Menu {
title: qsTr("&File")
MenuItem {
text: qsTr("&Open")
onTriggered: messageDialog.show(qsTr("Open action triggered"));
}
MenuItem {
text: qsTr("Save")
onTriggered: messageDialog.show(qsTr("Save action triggered"));
}
}
Menu {
title: qsTr("&Help")
MenuItem {
text: qsTr("About")
onTriggered: messageDialog.show(qsTr("About: test QML app with menu"));
}
}
}
MainForm {
anchors.fill: parent
button1.onClicked: messageDialog.show(qsTr("Button 1 pressed"))
button2.onClicked: messageDialog.show(qsTr("Button 2 pressed"))
}
MessageDialog {
id: messageDialog
title: qsTr("Message Test")
function show(caption) {
messageDialog.text = caption;
messageDialog.open();
}
}
}

Resources