【问题标题】:How to Implement an 'Open With' to Qt application?如何为 Qt 应用程序实现“打开方式”?
【发布时间】:2021-11-23 06:09:23
【问题描述】:

如何在 QT C++ 中创建一个文本编辑器,我可以通过右键单击它打开任何文本文档并使用我的应用程序打开它。

【问题讨论】:

标签: c++ windows qt


【解决方案1】:

首先接收文件路径作为位置参数 main.cpp

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QCommandLineParser parser;
    parser.addPositionalArgument("file", QCoreApplication::translate("main", "The file to open."));
    parser.process(a);
    MainWindow w;
    w.show();
    QStringList filename=parser.positionalArguments();
    w.OpenFileInText(filename[0]);
    return a.exec();
}

然后通过文件打开文件方法。 下面是MainWindow.cpp中的方法

void MainWindow::OpenFileInText(QString strFilePath)
{
    ui->plainTextEdit->clear();
    QFile file(strFilePath);
    if(!file.open(QIODevice::ReadOnly)) {
        QMessageBox::information(0, "error", file.errorString());
    }

    QTextStream in(&file);

    while(!in.atEnd())
    {
        QString line = in.readLine();
        ui->plainTextEdit->appendPlainText(line) ;

    }

    file.close();
}

【讨论】:

  • 这并没有解释如何将 "Open with my awesome" 选项注册到 Explorer 的上下文菜单中。
猜你喜欢
  • 2022-06-11
  • 2016-07-09
  • 2015-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多