-2

How I can create a text editor in QT C++ which I can open any text document by right-clicking it and open with my application .

Arun K
  • 413
  • 4
  • 15
  • 1
    That's not program's responsibility, not entirely at least. It's matter of registering\deploying and methods depend on OS. – Swift - Friday Pie Oct 01 '21 at 15:38
  • Search SO will give you to e.g. https://stackoverflow.com/questions/20449316/how-add-context-menu-item-to-windows-explorer-for-folders – chehrlic Oct 01 '21 at 17:08

1 Answers1

0

First receive the file path as positional argument 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();
}

Then pass the file to open file method. Below is the method in 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();
}
Arun K
  • 413
  • 4
  • 15
  • 1
    This doesn't explain how to actually get the *"Open with my awesome"* option registered into Explorer's context menu. – IInspectable Oct 01 '21 at 16:20