简体   繁体   中英

Is there any way to set the default file name of qfiledialog while saving file

as the description.I need to use the QFileDialog in Qt Quick Application without any widget . So I have to use it in QML. However, FileDialog in QML has no method/property to set the default saving file name so far as I know.

anyone has met the same problem with such requirement?

QML lacks such features. Check this for a possible solution.

EDIT:

The solution uses widget so add following in .pro file

QT += widgets

qmlfile.h

//qmlfile.h
#ifndef QMLFILE_H
#define QMLFILE_H

#include <QObject>

class QMLFile : public QObject
{
    Q_OBJECT

public:
    explicit QMLFile(QObject *parent = 0);

    Q_INVOKABLE QString getFileContents() const;

    Q_INVOKABLE void saveFileContents(QString fileContents) const;
};

#endif

qmlfile.cpp

#include <QFileDialog>
#include <QTextStream>
#include <QDebug>
#include "qmlfile.h"

QMLFile::QMLFile(QObject *parent): QObject(parent)
{

}

QString QMLFile::getFileContents() const
{
    QString fileName = QFileDialog::getOpenFileName(NULL, tr("Open File"), "/home", tr("Text Files (*.txt)"));
    qDebug() << "fileName:" << fileName;
    QFile file(fileName);
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return "";

    QString content = file.readAll();
    file.close();
    return content;
}

void QMLFile::saveFileContents(QString fileContents) const
{
    QString fileName = QFileDialog::getSaveFileName(NULL, tr("Save File"), "/home/ansh/data.txt", tr("Text Files (*.txt)"));  // This is were default file name is selected

    QFile file(fileName);
    if(file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        qDebug() << "created file:" << fileName;
        QTextStream stream(&file);
        stream << fileContents << endl;
        file.close();
        return;
    }
    else
    {
        qDebug() << "could not create file:" << fileName;
        return;
    }
}

main.cpp

#include <QGuiApplication>
#include <QQuickView>
#include <QQmlContext>
#include <QApplication>
#include "qmlfile.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QQuickView view;
    QMLFile qmlFile;
    view.rootContext()->setContextProperty("QMLFile", &qmlFile);
    view.setSource(QUrl(QLatin1String("qrc:/main.qml")));
    view.show();

    return app.exec();
}

main.qml

import QtQuick 2.5
import QtQuick.Controls 2.0

Rectangle {
    width: 360; height: 360

    Rectangle{
        id:buttons
        height: 50; width: parent.width; anchors.top: parent.top

        Button {
            anchors {
                left: parent.left
                leftMargin: 5
                top: parent.top
                topMargin: 5
            }
            text: "open"
            onClicked: txt.text = QMLFile.getFileContents();
        }

        Button {
            anchors {
                right: parent.right
                rightMargin: 5
                top: parent.top
                topMargin: 5
            }
            text: "save"
            onClicked: QMLFile.saveFileContents(txt.text);
        }
    }

    Rectangle{
        id:textHandle
        width: parent.width; height: parent.height - buttons.height; anchors.bottom: parent.bottom

        TextEdit{
            id: txt; anchors.fill: parent
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM