gpt4 book ai didi

windows - Qt - 通过双击打开自定义文件

转载 作者:可可西里 更新时间:2023-11-01 09:46:01 24 4
gpt4 key购买 nike

我有一个程序和一个包含几行信息的 .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 文件时,程序启动但不加载文件。我需要做什么才能使其正确加载?示例代码将不胜感激。

最佳答案

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

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

关于windows - Qt - 通过双击打开自定义文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10994339/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com