2

我有一个程序和一个包含一些信息行的 .l2p 文件。我已经运行了一个注册表文件:

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\""

当我双击 .l2p 文件时,程序启动但不加载文件。我该怎么做才能使其正确加载?示例代码将不胜感激。

4

2 回答 2

7

当您双击文件时,文件名将作为命令行参数传递给相关程序。您必须解析命令行,获取文件名并打开它(如何做到这一点取决于您的程序如何工作)。

#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;
    }
}

如果你从命令行运行这个程序:

>test.exe "path/to/file" "/path/to/second/file"
The 1th argument is path/to/file
The 2th argument is /path/to/second/file

在 Qt 中,如果您创建 QApplication,您还可以通过QCoreApplications::arguments()访问命令行参数。

您可能希望在创建主窗口后加载文件。你可以这样做:

#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();
}

这样MainWindow::initialize(),一旦事件循环开始,插槽(您必须定义)就会被调用。

void MainWindow::initialize()
{
    QStringList arguments = QCoreApplication::arguments();
    // Now you can parse the arguments *after* the main window has been created.
}
于 2012-06-12T12:21:19.677 回答
0

如果我正确理解了您的问题,L2P.exe您是否创建了一个 Qt 程序,并且您希望将传递的参数作为指定要打开的文件来处理。如果是这种情况,您只需要在您的main()方法中读取该参数并处理它。(这不是自动发生的事情。)如下所示,尽管您显然想添加一些错误检查:

int main(int argc, char *argv[]) {
  QApplication a(argc, argv);

  const QStringList arguments = a.arguments();

  // The final argument is assumed to be the file to open.
  if (arguments.size() > 1 && QFile::exists(arguments.last())) {
    your_app_open(arguments.last());
  }

  // ... etc.
}
于 2012-06-12T12:14:34.993 回答