Qt - Opening a custom file via double-click
loading, qt, windows
Solution
When you double click on a file the file name is passed as a command line argument to the associated program. You have to parse the command line, get the file name and open it (how to do that depends on how your program works).
#include <iostream>
int main(int argc, char *argv[])
{
for (int i = 1; i < argc; ++i) {
std::cout << "The " << i << "th argument is " << argv[i] << std::endl;
}
}
If you run this program from command line:
>test.exe "path/to/file" "/path/to/second/file"
The 1th argument is path/to/file
The 2th argument is /path/to/second/file
In Qt if you create a QApplication you can also access the command line arguments via QCoreApplications::arguments().
You might want to load the file after having created your main window. You may do something like this:
#include <QApplication>
#include <QTimer>
#include "MainWindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow window;
QTimer::singleShot(0, & window, SLOT(initialize()));
window.show();
return app.exec();
}
This way the slot `MainWindow::initialize()` (which you have to define) gets called as soon as the event loop has started.
void MainWindow::initialize()
{
QStringList arguments = QCoreApplication::arguments();
// Now you can parse the arguments *after* the main window has been created.
}
Problem
I have a program and a .l2p file with some lines of info. I have run a registry file: ``` Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\.l2p\DefaultIcon] @="\"C:\\Program Files\\ToriLori\\L2P.exe\",0" [HKEY_CLASSES_ROOT\.l2p\shell\Open\command] @="\"C:\\Program Files\\ToriLori\\L2P.exe\" \"%1\"" ``` When I double-click the .l2p file the program starts but doesn't load the file. What do I have to do to make it load properly? Example code would be very appreciated.