QString replace %U
-
OpenWith menu is done, open action (for example) should replace gedit %U to gedit filepath.
QString filepath = "*********"; QString s = linux_desktop_file.exec; s.replace(QString("%U"), filepath, Qt::CaseInsensitive); // error: no matching member function for call to 'replace'
@sonichy said in QString replace %U:
s.replace(QString("%U"), filepath, Qt::CaseInsensitive); // error: no matching member function for call to 'replace'
This has nothing to do with the
%U
part.You show a warning in the code editor. That depends on what Code Model you use. Show your line actually generating an error from the compiler when you build. If it compiles, there is nothing wrong with it.
-
@sonichy said in QString replace %U:
s.replace(QString("%U"), filepath, Qt::CaseInsensitive); // error: no matching member function for call to 'replace'
This has nothing to do with the
%U
part.You show a warning in the code editor. That depends on what Code Model you use. Show your line actually generating an error from the compiler when you build. If it compiles, there is nothing wrong with it.
-
Change connect lambda [=] to [&], error disappare, what is the difference ?
New problem: sExec always run the last whatever I click the first, second, third !"deepin-draw %U" "/usr/bin/display-im6.q16 -nostdin %F" "deepin-album %F" "/usr/bin/google-chrome-stable %U" "deepin-image-viewer --new-window %F" "deepin-image-viewer --new-window /home/sonichy/Desktop/KuGou.png"
-
Change connect lambda [=] to [&], error disappare, what is the difference ?
New problem: sExec always run the last whatever I click the first, second, third !"deepin-draw %U" "/usr/bin/display-im6.q16 -nostdin %F" "deepin-album %F" "/usr/bin/google-chrome-stable %U" "deepin-image-viewer --new-window %F" "deepin-image-viewer --new-window /home/sonichy/Desktop/KuGou.png"
@sonichy Read the official docs: https://en.cppreference.com/w/cpp/language/lambda, in special
Lambda capture
section.In simple words: with
=
you are passing values by copy, instead with&
you are passing it as a reference. In your initial case "sExec" inside the lambda is a copy of the original. On the other hand, it is not advisable to use&
or=
(its can cause bugs), it is better to indicate the variables explicitly:[&sExec]
-
Change connect lambda [=] to [&], error disappare, what is the difference ?
New problem: sExec always run the last whatever I click the first, second, third !"deepin-draw %U" "/usr/bin/display-im6.q16 -nostdin %F" "deepin-album %F" "/usr/bin/google-chrome-stable %U" "deepin-image-viewer --new-window %F" "deepin-image-viewer --new-window /home/sonichy/Desktop/KuGou.png"
@sonichy I recommend you not to abuse lambdas, a better option is to save the variables in a property to be able to execute it in a slot:
QString sExec= .... QString sPath = .... QAction *action = new QAction(...); action->setProperty("sExec", sExec); action->setProperty("sPath", sPath); // TODO connect(action, &QAction::triggered, this, &Foo::handle_triggered);
void Foo::handle_triggered(){ if(QAction *action = qobject_cast<QAction *>(sender())){ QString sExec= action->property("sExec").toString(); QString sPath = action->property("sPath").toString(); qDebug() << sExec << sPath; } }
-
@sonichy I recommend you not to abuse lambdas, a better option is to save the variables in a property to be able to execute it in a slot:
QString sExec= .... QString sPath = .... QAction *action = new QAction(...); action->setProperty("sExec", sExec); action->setProperty("sPath", sPath); // TODO connect(action, &QAction::triggered, this, &Foo::handle_triggered);
void Foo::handle_triggered(){ if(QAction *action = qobject_cast<QAction *>(sender())){ QString sExec= action->property("sExec").toString(); QString sPath = action->property("sPath").toString(); qDebug() << sExec << sPath; } }
@eyllanesc Move replace out of lambda work fine.
QString sExec = readSettings(desktop, "Desktop Entry", "Exec"); sExec.replace(QString("%U"), filepath, Qt::CaseInsensitive); sExec.replace(QString("%F"), filepath, Qt::CaseInsensitive); connect(action, &QAction::triggered, [=](){ QProcess *process = new QProcess; process->setWorkingDirectory(sPath); qDebug() << sExec; process->startDetached(sExec, QStringList()); });
-
@eyllanesc Move replace out of lambda work fine.
QString sExec = readSettings(desktop, "Desktop Entry", "Exec"); sExec.replace(QString("%U"), filepath, Qt::CaseInsensitive); sExec.replace(QString("%F"), filepath, Qt::CaseInsensitive); connect(action, &QAction::triggered, [=](){ QProcess *process = new QProcess; process->setWorkingDirectory(sPath); qDebug() << sExec; process->startDetached(sExec, QStringList()); });
-
@eyllanesc Move replace out of lambda work fine.
QString sExec = readSettings(desktop, "Desktop Entry", "Exec"); sExec.replace(QString("%U"), filepath, Qt::CaseInsensitive); sExec.replace(QString("%F"), filepath, Qt::CaseInsensitive); connect(action, &QAction::triggered, [=](){ QProcess *process = new QProcess; process->setWorkingDirectory(sPath); qDebug() << sExec; process->startDetached(sExec, QStringList()); });
-
@sonichy
Also you leak theQProcess *process = new QProcess;
. Assuming oncestartDetached()
has been called Qt no longer needs theQProcess
, try making it a stack instance, you'll have to make sure that works without issue. -
@sonichy said in QString replace %U:
Yes , the process do not start !
Under what circumstances? The way your code above is/was? After you have changed to make
QProcess
a stack variable? Both?Please try to give some clear detail with your posts. You give so little information, we have to keep asking you for more....
-
@sonichy said in QString replace %U:
Yes , the process do not start !
Under what circumstances? The way your code above is/was? After you have changed to make
QProcess
a stack variable? Both?Please try to give some clear detail with your posts. You give so little information, we have to keep asking you for more....
-
@sonichy said in QString replace %U:
This is new problem after modify the code
Fair enough, I did say you would have see whether it causes problems. This raises a nasty question of what Qt requires the scope/lifetime of the process used in
QProcess::startDetached()
to be: it cannot be "forever", because we know detached processes survive even exiting the parent program. Maybe it requires it to exist till at leaststarted()
signal has been received, I don't know.Actually, hang on! The overload of
startDetached
you are using is bool QProcess::startDetached(const QString &program, const QStringList &arguments, const QString &workingDirectory = QString(), qint64 *pid = nullptr). That isstatic
!Soooo:
-
process->setWorkingDirectory(sPath);
has no effect (the method signature above takes an optional parameter for the working directory instead). -
Your
QProcess *process = new QProcess;
is not used. -
You can write
QProcess::startDetached(sExec, QStringList());
instead, noQProcess
instance needed, and no leaking. -
Given this I find it hard to believe that changing
QProcess *process
toQProcess process
can have any effect....
-
-
@jsulm Click QAction QProcess do not start, copy qDebug() content to terminal can run.
QString sExec = readSettings(desktop, "Desktop Entry", "Exec"); sExec.replace(QString("%U"), filepath, Qt::CaseInsensitive); sExec.replace(QString("%F"), filepath, Qt::CaseInsensitive); connect(action, &QAction::triggered, [=](){ QProcess *process = new QProcess; process->setWorkingDirectory(path); qDebug() << sExec; //process->start(sExec); process->setProgram(sExec); process->startDetached(); });
-
@jsulm Click QAction QProcess do not start, copy qDebug() content to terminal can run.
QString sExec = readSettings(desktop, "Desktop Entry", "Exec"); sExec.replace(QString("%U"), filepath, Qt::CaseInsensitive); sExec.replace(QString("%F"), filepath, Qt::CaseInsensitive); connect(action, &QAction::triggered, [=](){ QProcess *process = new QProcess; process->setWorkingDirectory(path); qDebug() << sExec; //process->start(sExec); process->setProgram(sExec); process->startDetached(); });
@sonichy
IssExec
set to the correct full path to the executable? What do you think yourprocess->setWorkingDirectory(path);
achieves? Because if you think it is used to locate the executable to run, it is not.The
bool QProcess::startDetached(qint64 *pid = nullptr)
overload you are now using has a return value. Do you think you ought look at that if you are having a problem? -
@sonichy said in QString replace %U:
This is new problem after modify the code
Fair enough, I did say you would have see whether it causes problems. This raises a nasty question of what Qt requires the scope/lifetime of the process used in
QProcess::startDetached()
to be: it cannot be "forever", because we know detached processes survive even exiting the parent program. Maybe it requires it to exist till at leaststarted()
signal has been received, I don't know.Actually, hang on! The overload of
startDetached
you are using is bool QProcess::startDetached(const QString &program, const QStringList &arguments, const QString &workingDirectory = QString(), qint64 *pid = nullptr). That isstatic
!Soooo:
-
process->setWorkingDirectory(sPath);
has no effect (the method signature above takes an optional parameter for the working directory instead). -
Your
QProcess *process = new QProcess;
is not used. -
You can write
QProcess::startDetached(sExec, QStringList());
instead, noQProcess
instance needed, and no leaking. -
Given this I find it hard to believe that changing
QProcess *process
toQProcess process
can have any effect....
@JonB Two method:
bool QProcess::startDetached(qint64 *pid = nullptr)[static] bool QProcess::startDetached(const QString &program, const QStringList &arguments, const QString &workingDirectory = QString(), qint64 *pid = nullptr)
And static do not start either.
QProcess::startDetached(sExec, QStringList(), path);
-
-
@JonB Two method:
bool QProcess::startDetached(qint64 *pid = nullptr)[static] bool QProcess::startDetached(const QString &program, const QStringList &arguments, const QString &workingDirectory = QString(), qint64 *pid = nullptr)
And static do not start either.
QProcess::startDetached(sExec, QStringList(), path);
-
@JonB Two method:
bool QProcess::startDetached(qint64 *pid = nullptr)[static] bool QProcess::startDetached(const QString &program, const QStringList &arguments, const QString &workingDirectory = QString(), qint64 *pid = nullptr)
And static do not start either.
QProcess::startDetached(sExec, QStringList(), path);
-
@sonichy
Then I do not see that anything you have had so far can have succeeded, but it's most unclear from your comments what that case is.As per my previous, could you please show what your
qDebug() << sExec;
is outputting, instead of making us guess? -
@sonichy said in QString replace %U:
deepin-draw
This is a relative path. Pass whole path, so it can actually be found...
And please add error handling...