Qt-删除文件

Far*_*our 10 qt

我需要删除一个特定的文件.我试过这个:

msgBox.setButtonText(QMessageBox::Ok , tr("Ok"));
msgBox.setButtonText(QMessageBox::Cancel , tr("Cancel"));

int ret = msgBox.exec();

switch (ret)
{
    case QMessageBox::Ok:
    {
    #ifdef Q_OS_IOS
        QStringList paths = QStandardPaths::standardLocations(QStandardPaths::DataLocation);
        QString dbFile = paths.first().append("/log.dat");
    #else
        QString dbFile = "log.dat";
    #endif
        QSettings settings(CGlobalZone::m_companyName, CGlobalZone::m_softwareName);
        settings.clear();
        QDir dir;
        dir.remove(dbFile);

        break;

    case QMessageBox::Cancel:
        QTimer::singleShot(1500, this, SLOT(close()));
        break;
}
Run Code Online (Sandbox Code Playgroud)


但遗憾的是dbFile仍然存在.我怎么能删除"dbFile"?

小智 24

使用

    QFile file (dbFile);
    file.remove();
Run Code Online (Sandbox Code Playgroud)

代替

  QDir dir;
   dir.remove(dbFile);
Run Code Online (Sandbox Code Playgroud)


Ale*_*ph0 16

在 Qt 中删除文件的一个更简短的解决方案可能是:

QFile::remove(dbFile);
Run Code Online (Sandbox Code Playgroud)