如何使用QFileDialog :: getsaveFileName来保存带有QFormBuilder的ui文件?

时间:2014-03-11 20:28:53

标签: qt qt-creator

我想要的是什么:

当我调用save()时,我想使用QFileDialog来获取文件,并使用QFormBuilder保存ui文件(因为它允许我保存Qdesigner可识别的ui文件)

我有什么:

我有一个名为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);

} 

但是savef不是QIODevice,Qt正在抱怨它。

知道我该怎么办吗?

感谢。

1 个答案:

答案 0 :(得分:1)

您需要创建一个QFile并将其传递给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 
}

使用Qt 5.1或更新时,我会改为使用QSaveFile

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
}