We now have a working text editor that can open and save files. We might, however, want to extend its utility by enabling users to more quickly and efficiently use it to edit files. In this tutorial we will make the editor act more like a desktop application by enabling it to open files from command line arguments or even using Open with from within Dolphin.
This way, we can call openFile() if we want to display a dialog, or we can call openFileFromUrl(const QUrl &) if we know the name of the file already. Which will be the case when we feed the file name through the command line.
main.cpp
#include <QApplication>
#include <QCommandLineParser>
#include <QDir>
#include <QUrl>
#include <KAboutData>
#include <KLocalizedString>
#include "mainwindow.h"
int main (int argc, char *argv[])
{
using namespace Qt::Literals::StringLiterals;
QApplication app(argc, argv);
KLocalizedString::setApplicationDomain("texteditor");
KAboutData aboutData(
u"texteditor"_s,
i18n("Text Editor"),
u"1.0"_s,
i18n("A simple text area using QAction etc."),
KAboutLicense::GPL,
i18n("(c) 2024"),
i18n("Educational application..."),
u"https://apps.kde.org/someappname/"_s,
u"submit@bugs.kde.org"_s);
aboutData.addAuthor(
i18n("John Doe"),
i18n("Tutorial learner"),
u"john.doe@example.com"_s,
u"https://john-doe.example.com"_s,
u"johndoe"_s);
KAboutData::setApplicationData(aboutData);
QCommandLineParser parser;
aboutData.setupCommandLine(&parser);
parser.addPositionalArgument(u"file"_s, i18n("Document to open"));
parser.process(app);
aboutData.processCommandLine(&parser);
MainWindow *window = new MainWindow();
window->show();
if (parser.positionalArguments().count() > 0) {
window->openFileFromUrl(QUrl::fromUserInput(
parser.positionalArguments().at(0),
QDir::currentPath()));
}
return app.exec();
}
parser.addPositionalArgument(u"file"_s, i18n("Document to open"));
if (parser.positionalArguments().count() > 0) {
window->openFileFromUrl(QUrl::fromUserInput(parser.positionalArguments().at(0), QDir::currentPath()));
}
You should then see your application run and load testfile.txt directly from its UI, showing "It works!" in your textArea.
Here we have done nothing but add a new openFileFromUrl() function which takes a . Again, we use URLs instead of strings so that we can also work with remote files as if they were local.
There's no new code here, only rearranging. Everything from void openFile() has been moved into void openFileFromUrl(const QUrl &inputFileName) except the call to .
This is where all the magic happens. In previous examples, we only used the class to feed the necessary data for using flags like --version or --author. Now we actually get to use it to process command line arguments.
First, we tell that we want to add a new positional argument. In a nutshell, these are arguments that are not options. -h or --version are options, file is an argument.
Later on, we start processing positional arguments, but only if there is one. Otherwise, we proceed as usual. In our case we can only open one file at a time, so only the first file is of interest to us. We call the openFileFromUrl() function and feed it the URL of the file we want to open, whether it is a local file like "$HOME/foo" or a remote one like "ftp.mydomain.com/bar". We use the overloaded form of in order to set the current path. This is needed in order to work with relative paths like "../baz".