简体   繁体   中英

QFileDialog: How to open/select file/dir from package in osX

I have googled a lot but dint find any relative solution for my problem.

PROBLEM: I want to open .MTS file and its working find if its available in any directory . But if its in any package then my QFileDialog is not able to look into that package and select those .MTS files.

CODE:

auto filePaths = QFileDialog::getOpenFileNames(this, "Open Video File", lastOpenedPath, "*.MTS;*.mov");
    qDebug() << "File Paths " << filePaths;

Now the .MTS files created under AVCHD(Advanced Video Coding High Definition) package default in Sony & Panasonic HD Camera, and I want to import/select that .MTS files.

HINT: QFileDialog is able to import/select those .MTS files in Windows machine, but fail to import/select in mac machine.

Highly appreciated any thoughts.

Thanks.

Well, if I understand what you want to do properly, I'm not sure that it's possible in Qt alone.

It did turn out to be easier than I expected to simply call in to Cocoa and NSOpenPanel to achieve what I think you're looking for.

Sample project is at: https://github.com/NSGod/widgetsOpenFileDialogCocoa

Basically, I renamed mainwindow.cpp to mainwindow.mm , then added an #import <Cocoa/Cocoa.h> :

#import <Cocoa/Cocoa.h>

void MainWindow::on_openFileButton_clicked()
{
    NSOpenPanel *openPanel = [NSOpenPanel openPanel];
    [openPanel setAllowedFileTypes:[NSArray arrayWithObjects:@"mts", @"mov", nil]];
    [openPanel setAllowsMultipleSelection:YES];
    [openPanel setTreatsFilePackagesAsDirectories:YES];
    [openPanel setTitle:@"Open Video File"];

    NSInteger result = [openPanel runModal];

    QStringList stringList;

    if (result == NSFileHandlingPanelOKButton) {
        NSArray *URLs = [openPanel URLs];
        NSLog(@"URLs == %@", URLs);
        for (NSURL *URL in URLs) {
            stringList += QString::fromNSString(URL.path);
        }
        // do something with stringList
        qDebug() << "filePaths == " << stringList;
    }
}

Included in the project is a fakeBundle.component directory, which will be treated as a bundle (or "package") by OS X. But by setting treatsFilePackagesAsDirectories to YES, you can have the NSOpenPanel treat it as a directory (which it really is, of course).

Here is an image showing how the Finder treats this fakeBundle.component directory as if it were a single file:

在此输入图像描述

And here in the NSOpenPanel , it's being treated as a directory:

在此输入图像描述

An OSX package is a:

File system directory that is normally displayed to the user by the Finder as if it were a single file. Such a directory may be the top-level of a directory tree of objects stored as files, or it may be other archives of files or objects for various purposes, such as installer packages, or backup archives.

This is akin to an .mst or .msi file on Windows . Just as with OSX packages you would not be able to open your specified file within one of these packages. Your system open dialog is in fact doing you a disservice by allowing you to see into them, as you cannot open said files.

Your work around is to copy the file out of the package externally to the program then open the copy of the file.

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