Planned maintenance has been done but it did not solve the problem. So work will continue on this and a new time for trying updates will be announced asap.

How can I set the string QLineEdit in application with help of other application or shell script?



  • I would like to set the string in QLineedit from shell script whenever my application is run. How can i call the QlineEdit of the application from external application or shell script?

    Is it any way to do it in Qt?



  • @Yash001
    Do you mean: you want your application to be passed a parameter on the command-line, and then you put that into a QLineEdit you show?



  • Yes exactly @JonB .

    if any change is detect in list qApp->arguments() then i would like to put string qApp->arguments().at(1) inside the QLineEdit.


  • Lifetime Qt Champion

    Hi,

    QCommandLineParser ?

    Where is your QLineEdit located ?



  • @SGaist Thank you for direction.

    QLineEdit is located inside MainWindow.cpp file. I am trying to change path from main.cpp file. I create the instance of MainWindow inside the main.cpp, and calling member function of MainWindow for changing string in QLineEdit.

    In addition, Users are not able open multiple instance of application.

    Now, I am able to set the argument inside the QLineEdit. whenever I open first time my application.

    But if second time user will call application with command line arguments, at that time it is open old instance of application, and it is correct but it won't change the string in QLineEdit.

    I need some kind of functionality which is detect the change in qApp->arguments() list and notify to QLineEdit for changing string.


  • Lifetime Qt Champion

    What are you using to have a single instance of your application ?


  • Qt Champions 2018

    @Yash001

    I need some kind of functionality which is detect the change in qApp->arguments() list and notify to QLineEdit for changing string.

    There is no «change of arguments». every program has its own set of arguments, and they are fully separated.

    the only thin you can do is, from the second prog instance detect that there is already an instance running and send the new strings by inter-process communication.



  • @SGaist I am using windows API and Last save name of application to identify the running instance.

    QStringList GetProcessList() {
    	QStringList ret;
    
    	QProcess process;
    	process.setReadChannel(QProcess::StandardOutput);
    	process.setReadChannelMode(QProcess::MergedChannels);
    	process.start("wmic.exe /OUTPUT:STDOUT PROCESS get Caption");
    	process.waitForStarted(1000);
    	process.waitForFinished(-1);
    
    
    	QTextStream txtStr(&process);
    
    	while (!txtStr.atEnd()) {
    		auto line = txtStr.readLine().trimmed();
    		ret << line;
    	}
    	
    	return ret;
    }
    

    this code will return the QstringList, which is contain QFileInfo(qApp->applicationFilePath()).fileName(); if application is running.



  • @aha_1980 Thank you for direction. let me try.


  • Lifetime Qt Champion

    You should consider using QtSingleApplication. It's meant for that kind of cases.



  • @Yash001
    The fact remains that regardless of the whole single instance issue, as @aha_1980 has said there is no capability to "change command line of a running application". qApp->arguments() will never return a different result no matter what you try to do. Unless you restart your application for a new string, the only way to approach this will be some kind of IPC connecting to the running program to alter anything in it.



  • @Yash001 have you considered using command line arguments?



  • @mzimmers Yes, With help of Shell script I am sending string to my application. It will updated int argc, char *argv[] on another instance.



  • @JonB Yup. I am working on that approach. Thank you.



  • @SGaist Thank you inform about me QtSingleApplication class. I will modify code and use QtSingleApplication.