在再次打开文件之前知道文件当前是否打开的方法?

Lio*_*ing 1 c++ qt qt5

我有一个程序可以多次打开同一个文件.
如果此文件当前是打开的,我想在打开任何文件之前检查,因为我不想多次打开同一个文件.

在此输入图像描述

是否有内置函数可以检查文件当前是否打开或任何其他方式可以做到这一点?

代码:

QString openFilePath = QFileDialog::getOpenFileName(this->mainWindow, "Open File");
if(openFilePath == ""){
    return;
}
QFile openFile(openFilePath);
if(!openFile.open(QFile::ReadWrite)){
    QMessageBox::critical(this->mainWindow, "Can't Open file", "Can't access to the file.");
}
QTextStream fileContent(&openFile);
QFileInfo fileInfo(openFile);
this->createEmptyFile(fileInfo.fileName());
this->txtEditor->setText(fileContent.readAll());
Run Code Online (Sandbox Code Playgroud)

Eme*_*pon 6

在我看来,你的问题实际上与编程意义上的文件打开无关,而是与你的应用程序逻辑完全相关.您需要在内部保留所有当前打开的文件的列表(在您的GUI显示此类文件的意义上),并检查用户是否打开新文件.