how to open a file in my qt app via double clicking on it
-
Can you try again using QCommandLineParser ?
-
Can you try again using QCommandLineParser ?
@SGaist how can i use QCommandLineParser to get application's arguments
-
@SGaist how can i use QCommandLineParser to get application's arguments
@_-mohamed-_ See the official example: https://doc.qt.io/qt-5/qcommandlineparser.html#details
-
@eyllanesc
@SGaist
i cant get application's arguments using QCommandLineParser -
@eyllanesc
@SGaist
i cant get application's arguments using QCommandLineParser@_-mohamed-_ Basic rule of the forum: Without code we cannot help you. Show the code where you implement the logic with
QCommandLineParser
, so we can analyze where the error is and so we can probably give you a solution. -
@_-mohamed-_ Basic rule of the forum: Without code we cannot help you. Show the code where you implement the logic with
QCommandLineParser
, so we can analyze where the error is and so we can probably give you a solution.@eyllanesc I just don't know how to do that
I tried
QCommandLineParser command; QStringList arguments = command.positionalArguments(); if(arguments.count() > 1) { QFile file(arguments.at(1)); if(!file.open(QFile::ReadOnly | QFile::Text)) QMessageBox::information(this, "can't open file", "can't open the specified file"); else { QTextStream text(&file); text.setCodec("UTF-8"); QString fileContents = text.readAll(); ui->TextEdit->setPlainText(fileContents); file.close(); edited = false; url = arguments.at(1); changeTitle(); } }
-
@eyllanesc I just don't know how to do that
I tried
QCommandLineParser command; QStringList arguments = command.positionalArguments(); if(arguments.count() > 1) { QFile file(arguments.at(1)); if(!file.open(QFile::ReadOnly | QFile::Text)) QMessageBox::information(this, "can't open file", "can't open the specified file"); else { QTextStream text(&file); text.setCodec("UTF-8"); QString fileContents = text.readAll(); ui->TextEdit->setPlainText(fileContents); file.close(); edited = false; url = arguments.at(1); changeTitle(); } }
@_-mohamed-_ Your sentence: i cant get application's arguments using QCommandLineParser is confusing. You should have said I don't know how to implement, why don't you use the official example? Try it and when it fails, they receive goods with the code that does not work.
-
@_-mohamed-_ Your sentence: i cant get application's arguments using QCommandLineParser is confusing. You should have said I don't know how to implement, why don't you use the official example? Try it and when it fails, they receive goods with the code that does not work.
@eyllanesc
when i use args likeqDebug() << args.at(0);
i got thisThe program has unexpectedly finished.
-
@eyllanesc
when i use args likeqDebug() << args.at(0);
i got thisThe program has unexpectedly finished.
@_-mohamed-_ what is
args
?, please provide a minimal and verifiable example. -
@_-mohamed-_ what is
args
?, please provide a minimal and verifiable example.@eyllanesc it is from the official example
const QStringList args = parser.positionalArguments(); // source is args.at(0), destination is args.at(1)
you can find it here:
int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); QCoreApplication::setApplicationName("my-copy-program"); QCoreApplication::setApplicationVersion("1.0"); QCommandLineParser parser; parser.setApplicationDescription("Test helper"); parser.addHelpOption(); parser.addVersionOption(); parser.addPositionalArgument("source", QCoreApplication::translate("main", "Source file to copy.")); parser.addPositionalArgument("destination", QCoreApplication::translate("main", "Destination directory.")); // A boolean option with a single name (-p) QCommandLineOption showProgressOption("p", QCoreApplication::translate("main", "Show progress during copy")); parser.addOption(showProgressOption); // A boolean option with multiple names (-f, --force) QCommandLineOption forceOption(QStringList() << "f" << "force", QCoreApplication::translate("main", "Overwrite existing files.")); parser.addOption(forceOption); // An option with a value QCommandLineOption targetDirectoryOption(QStringList() << "t" << "target-directory", QCoreApplication::translate("main", "Copy all source files into <directory>."), QCoreApplication::translate("main", "directory")); parser.addOption(targetDirectoryOption); // Process the actual command line arguments given by the user parser.process(app); const QStringList args = parser.positionalArguments(); // source is args.at(0), destination is args.at(1) bool showProgress = parser.isSet(showProgressOption); bool force = parser.isSet(forceOption); QString targetDir = parser.value(targetDirectoryOption); // ... }
-
@eyllanesc it is from the official example
const QStringList args = parser.positionalArguments(); // source is args.at(0), destination is args.at(1)
you can find it here:
int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); QCoreApplication::setApplicationName("my-copy-program"); QCoreApplication::setApplicationVersion("1.0"); QCommandLineParser parser; parser.setApplicationDescription("Test helper"); parser.addHelpOption(); parser.addVersionOption(); parser.addPositionalArgument("source", QCoreApplication::translate("main", "Source file to copy.")); parser.addPositionalArgument("destination", QCoreApplication::translate("main", "Destination directory.")); // A boolean option with a single name (-p) QCommandLineOption showProgressOption("p", QCoreApplication::translate("main", "Show progress during copy")); parser.addOption(showProgressOption); // A boolean option with multiple names (-f, --force) QCommandLineOption forceOption(QStringList() << "f" << "force", QCoreApplication::translate("main", "Overwrite existing files.")); parser.addOption(forceOption); // An option with a value QCommandLineOption targetDirectoryOption(QStringList() << "t" << "target-directory", QCoreApplication::translate("main", "Copy all source files into <directory>."), QCoreApplication::translate("main", "directory")); parser.addOption(targetDirectoryOption); // Process the actual command line arguments given by the user parser.process(app); const QStringList args = parser.positionalArguments(); // source is args.at(0), destination is args.at(1) bool showProgress = parser.isSet(showProgressOption); bool force = parser.isSet(forceOption); QString targetDir = parser.value(targetDirectoryOption); // ... }
@_-mohamed-_ How have you run your executable? You must run:
your_executable input_filename output_filename
. -
@_-mohamed-_ How have you run your executable? You must run:
your_executable input_filename output_filename
.@eyllanesc
I run the app from qt creatorbut the first argument should be the exe file path and it does not need second argument.
It should work when i call the first argument -
@eyllanesc
I run the app from qt creatorbut the first argument should be the exe file path and it does not need second argument.
It should work when i call the first argument@_-mohamed-_ No, precisely QCommandLineParser considers that it is not necessary and only obtains the arguments, not the name of the program. If you want to know the name of the executable then use
QFileInfo(QCoreApplication::applicationFilePath()).fileName()
.The format is:
executable arg1 arg2 ... argN
The executable name is not a program argument for
QCommandLineParser
. -
@_-mohamed-_ No, precisely QCommandLineParser considers that it is not necessary and only obtains the arguments, not the name of the program. If you want to know the name of the executable then use
QFileInfo(QCoreApplication::applicationFilePath()).fileName()
.The format is:
executable arg1 arg2 ... argN
The executable name is not a program argument for
QCommandLineParser
.@eyllanesc oh,
I thought it was likeQCoreApplication::arguments()
I need the second argument which is the path of the file I want to open
-
@eyllanesc oh,
I thought it was likeQCoreApplication::arguments()
I need the second argument which is the path of the file I want to open
@_-mohamed-_ If you want to pass arguments using Qt Creator then click on the "Projects" on the left side and press the "Run" button, then put the arguments in "Command Line Arguments"
-
@_-mohamed-_ If you want to pass arguments using Qt Creator then click on the "Projects" on the left side and press the "Run" button, then put the arguments in "Command Line Arguments"
@eyllanesc oh thanks this information will help me and save my time.
I tried to get the argument using QCommandLineParser, I got the same result
(C:\Users\moham\OneDrive\Desktop\????.txt
)note: when I open the same file using windows notepad it workes fine.
this is main.cpp:
#include "mainwindow.h" #include <QApplication> #include <QDebug> using namespace std; int main(int argc, char *argv[]) { QApplication a(argc, argv); QCommandLineParser parser; parser.process(a); QStringList args = parser.positionalArguments(); MainWindow w; w.settext(args.at(0)); w.show(); return a.exec(); }
and this is settext() function from mainwindow.cpp:
void MainWindow::settext(QString URL) { QStringList arguments = QCoreApplication::arguments(); if(arguments.count() > 1) { QFile file(URL); if(!file.open(QFile::ReadOnly | QFile::Text)) QMessageBox::information(this, "can't open file", "can't open the specified file"); else { QTextStream text(&file); text.setCodec("UTF-8"); QString fileContents = text.readAll(); ui->TextEdit->setPlainText(fileContents); file.close(); edited = false; url = URL; changeTitle(); } } }
when i double click the text file i got error(
QMessageBox::information(this, "can't open file", "can't open the specified file");
) -
@eyllanesc oh thanks this information will help me and save my time.
I tried to get the argument using QCommandLineParser, I got the same result
(C:\Users\moham\OneDrive\Desktop\????.txt
)note: when I open the same file using windows notepad it workes fine.
this is main.cpp:
#include "mainwindow.h" #include <QApplication> #include <QDebug> using namespace std; int main(int argc, char *argv[]) { QApplication a(argc, argv); QCommandLineParser parser; parser.process(a); QStringList args = parser.positionalArguments(); MainWindow w; w.settext(args.at(0)); w.show(); return a.exec(); }
and this is settext() function from mainwindow.cpp:
void MainWindow::settext(QString URL) { QStringList arguments = QCoreApplication::arguments(); if(arguments.count() > 1) { QFile file(URL); if(!file.open(QFile::ReadOnly | QFile::Text)) QMessageBox::information(this, "can't open file", "can't open the specified file"); else { QTextStream text(&file); text.setCodec("UTF-8"); QString fileContents = text.readAll(); ui->TextEdit->setPlainText(fileContents); file.close(); edited = false; url = URL; changeTitle(); } } }
when i double click the text file i got error(
QMessageBox::information(this, "can't open file", "can't open the specified file");
)@_-mohamed-_
So why not debug outURL
and the file path fromQFile file
for yourself? Do you mean that filename really has????
in it?? -
@_-mohamed-_
So why not debug outURL
and the file path fromQFile file
for yourself? Do you mean that filename really has????
in it??@JonB no the real file name is "عربي.txt" but i can't get the real name from app's argument
-
@JonB no the real file name is "عربي.txt" but i can't get the real name from app's argument
@_-mohamed-_
Look atfile.errorString()
when thefile.open()
fails. And print outURL
andfile.fileName()
. -
@_-mohamed-_
Look atfile.errorString()
when thefile.open()
fails. And print outURL
andfile.fileName()
.@JonB i printed the URL and i got (C:\Users\moham\OneDrive\Desktop????.txt) as i said in the previous post
Sofile.open ()
can't open a strange text (????.txt)