How to use QFileDialog to open virtual folder - c++

To choose the folder or file which will be deleted or created on local machine, we can use the class QFileDialog.
But my problem is, my folder or file or not exist on local machine, but on the cloud. I mean, we can create a HTTP request to get the folder structure from the cloud, but I don't know what is the easiest way to display it on GUI as a QFileDialog. How to solve this problem?

You can use QFileDialog::getOpenFileUrl to select a folder or file from a remote location.
Please note that this is not portable: Qt, when possible, will use the native file dialog, which must support selecting remote files to begin with.
The non-native dialog supports only local files, as of version 5.5.
For a portable solution, the only way I see it working is to implement your own, using a QTreeView.

Related

Qt application has unexpected QDir::currentPath

I am using QSettings in my Qt application to store prefences and all was fine until I start to run it on Linux mint 19. Default location for settings file is application folder, but now it is creating in users home folder.
QDir::currentPath() returns home folder, if I am running application by double click, and correct executable location if I am running it from terminal. I have never seen this before. Maybe somebody knows how to fix it and run application by double click from it's own folder.
Don't depend on current working directory (returned by QDir::currentPath()), because that can be basically anything. Looks like your desktop environment's file manager sets current directory to home folder when you run software by double clicking, and this is generally sensible, because that is usually what user would want, when opening files from the app etc.
If I read correctly what you want, that is path to application folder to store settings in, you need QCoreApplication::applicationDirPath(). But note that doing this will force you to install the application to user home folder (because you can't write to system folders like /user/local/bin, and you shouldn't change this by changing permissions, either).
Based on comments, it seems QCoreApplication::applicationDirPath() gives wrong directory, but QCoreApplication::applicationFilePath() gives correct path. If that is really the case, then take the file path, and remove the executable file from it. However, looking at the Qt source, this shouldn't be possible because applicationDirPath() already does just that...
I recommend you instead use QSettings as documented in its basic usage.

How to specify a standard directory in a Qt project file

I have developed an application that I plan to deploy on Windows, Mac, and Linux. The program requires access to some files (scripts and the like) at run-time.
The installation process should install the files to a location that my application can later determine without user-intervention (or perhaps just a prompt allowing the user to change the location, if desired).
What is the best way to achieve this? I can't seem to find any way to:
1. Use a "standardized path" variable in the project file's INSTALLS statement. (e.g., my application could use QStandardPaths to initialize the location, but I can't figure out how to access this path from the INSTALLS statement)
2. Save the path to my project's QSettings (.plist, registry, whatever) for later retrieval
That leaves me with creating a custom project file and INSTALLS command for each environment, and then I still can't install to the user's directory because I don't know the user's name when I deploy the make command. It seems as if there must be a better way, but I can't seem to find any documentation for this. Am I just using the wrong keywords in my searches? Thanks in advance!
What standard directory? What type of getting that standard directory?
For instance, you can put such thing in your windows branch of .pro file:
win32 {
APPDATA_DIR = $$system(echo %APPDATA%) # should be %LOCALAPPDATA% as requested
message($$APPDATA_DIR)
}
Just unsure of what exact kind of standartized path you are talking about. QStandardPaths knows many. It makes sense to be more concrete to find the correspondence with concrete OS.
Also somewhat relative reply on mine, on how to check the correspondence with certain variable, etc: Qt .pro file - how to add conditioning on OSX version?
Maybe this class will help you
QStandardPaths documentation
But your problem is still little bit unclear for me.

QFileDialog not showing spool folder

I'm requesting the user to select a folder with QFileDialog:
QString directory = QFileDialog::getExistingDirectory(this,"Caption","",
QFileDialog::ShowDirsOnly);
I want the user to be able to select ALL folders, however C:/Windows/System32/spool/ cannot be found with the QFileDialog and i suspect there might be others.
I have tried setting the flag QFileDialog::HideNameFilterDetails in order to view hidden files, however this does not do the trick.
Is there a solution to this problem?
This appears to be a problem when you're running a 32-bit application on 64-bit Windows. You should be able to see the spool folder if you compile your application with the native x64 compiler. Worst case, you can write a simple 64-bit native app for showing the folder browser and have the 32-bit app run and communicate with the 64-bit app to get the results.
I was hoping that disabling the WOW64 File System Redirector would be enough, but it didn't help. According to this answer, it may work if you use Wow64DisableWow64FsRedirection to disable redirection on all the threads in the process but this approach is not recommended even by the person who answered the question.
You can't use that static function to see all folders. QFileDialog is doing some additional filtering behind the scenes, and that this filtering cannot be turned off in any obvious way using static function getExistingDirectory.
You can see all folders including the hidden ones by:
QFileDialog fd;
fd.setFilter(QDir::Hidden);
fd.setFileMode(QFileDialog::Directory);
fd.exec();
QString directory = fd.directory().path();

Is it possible to add a version number to a file that will be visible in Properties/Details in windows explorer

I have an unmanaged C++ project where I am writing data to a custom file format that I have defined.
What I would like to know is if there is a way to add a header that is compatible with Windows Explorer so that a version number will be displayed, as in the example below showing a Windows font.
The purpose of this is so that non-tech savvy users could simply right click and identify the version of the file, without having to open it in Notepad etc.
Any help would be appreciated.
Tom
You cannot achieve this for a file in general. But if your file format stores a version information, you can teach the Windows Explorer to display it.
You have to write a Shell Extension for the Explorer, that can extract arbitrary information out of your files. This extension must be installed on the target computer and registered in the registry.
An excellent guide on how to write and register Shell Extensions can be found here on CodeProject. (Part VIII should cover what you need)
The version information comes from VERSIONINFO resource, attached to a binary file, such as .EXE or .DLL. So it is easy to link such resource into your build target, this resource is also editable.
However, this is limited to the binary executable files, and you cannot attach this resource information to arbitrary files (as you wanted), including such as text files.
In VC just go to menu Project->AddResource and pick Version.

Find out the mime type and associated applications with Qt

How can I find out a mime-type or content-type of a given file?
I cannot use the suffix because the file could be renamed.
Possible additions would be categorizing them as jpg, gif, png and so on are image files and can be opened by editing applications, which has been set in the OS.
Thank you in advance.
What platform? On *nix, you should refer to how the program file does it, which is based on a few heuristics including checks of the first few bytes of a file (many file formats start with a fixed header, including many image formats).
If you're on Windows, the *nix file command is probably still instructive, even if you can't reuse its code as directly. There may also be some better solution in the Windows APIs (I'm not a Windows programmer).
This could help, it is with C# but I think you can get the idea.
http://kseesharp.blogspot.com/2008/04/c-get-mimetype-from-file-name.html
You can use some sort of class for acccesing Windows Registry from qt or using the Windows API directly from qt.
I am not a qt programmer.
You can't, not from within Qt.
However, if all you want is to show a file with the correct application, you can use QDesktopServices::openUrl(), which wraps open (Windows/OSX) and xdg-open (Unix). The URL may also be a local file (in that case, use QUrl::fromLocalFile() to construct the URL).
For categorizing image files. For Qt and just only for image files.
QByteArray imageFormat = QImageReader::imageFormat(fileName); //Where fileName - path to your file
Further mapping imageFormat to mime-type is not complicable
It doesn't look like Qt offers that capability yet. However, you may want to look into the libmagic library which does what the file and other similar commands do. Assuming the library is maintained properly it will include new MIME types as time passes. From what I can tell it is part of the file tool suite. Under a Debian system do something like this:
sudo apt-get install libmagic-dev
to get the development library and use #include to make use of it.