Question

What I want:

When I call save(), I would like to use the QFileDialog to get the file, and save ui file with QFormBuilder(because it lets me save ui files recognizable by Qdesigner)

What I have:

I have a method called save()

void MainWindow::save()
{
    QString savef = QFileDialog::getSaveFileName(this, tr("Save"), "file", tr("UI files (*.ui)"));
    //here down I would like to use the savef to save ui file
    QFormbuilder builder;
    builder.save(savef, myui);

} 

But savef is not QIODevice, and the Qt is complaining about it.

Any idea how I can do it?

Thanks.

Was it helpful?

Solution

You need to create a QFile and pass that to save():

QFile out(savef);
if (!out.open(QIODevice::WriteOnly)) {
    const QString error = tr("Could not open %1 for writing: %2").arg(savef, out.errorString());
    //report the error in some way...
    return;
}

builder.save(&out, myui);

const bool flushed = out.flush();

if (!flushed || out.error() != QFile::NoError) { // QFormBuilder lacks proper error reporting...
    const QString error = tr("Could not write form to %1: %2").arg(savef, out.errorString());
    //report error 
}

When using Qt 5.1 or newer, I'd use QSaveFile instead:

QSaveFile out(savef);

  if (!out.open(QIODevice::WriteOnly)) {
    const QString error = tr("Could not open %1 for writing: %2").arg(savef, out.errorString());
    //report the error in some way...
    return;
}

builder.save(&out, myui);

if (!out.commit()) {
    //report error
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top