How to open a file with Qt when there is no default program for it?

Viewed 317

There is a QDesktopServices::openUrl function in Qt which opens files with default programs, like when you want to open .docx file with Microsoft Office. However, the function will simply return 0 and do nothing if there is no default program assotiated with the file extension of the requested file. I would like Qt to show something like this instead:

enter image description here

A cross-platform solution would be ideal.

Is it possible with Qt?

2 Answers

This one works for me. But I didn't test it anywhere except my Windows 7 machine

QDesktopServices::openUrl(QUrl::fromLocalFile("D:/file"));

Try something like this. It should open the dialog you need. But on WIndows 10 it does not show the checkbox, I am not sure why.

#include <ShlObj.h>

bool openWith(const QString &filePath)
{
    QString nativePath = QDir::toNativeSeparators(filePath);
    OPENASINFO oi = {};
    oi.pcszFile = reinterpret_cast<LPCWSTR>(nativePath.utf16());
    oi.oaifInFlags = OAIF_ALLOW_REGISTRATION | OAIF_EXEC;
    return SHOpenWithDialog(NULL, &oi) == S_OK;
}
Related