PDA

View Full Version : Running External c++ program when user hit ok button



Ashwani
5th May 2010, 07:52
hello ...
I want to create a gui which runs a c++ program
i have a code here ...

QProcess proc;
proc.start("proc.exe");
proc.waitForStarted();
proc.write("3\n"); // input from a radio button as i have switch case in my c++ program
proc.write("7\n"); // input from a radio button as i have need integer between 1-9 as cut off
proc.write("file\n"); //file upload for reading by program ....
proc.waitForFinished();

but i want the proc.write to be user defined from my gui ....



my program needs these 3 inputs from command line ...
please help ..

thanks in advance ....

JD2000
5th May 2010, 14:57
but i want the proc.write to be user defined from my gui ....

I'm not entirely sure what you mean by this -
but assuming that you want the user to dictate what gets written, you could simply catch user input in a QlineEdit or something similar and use its text as the arguement to the proc.write function.

Talei
5th May 2010, 15:50
I think what You want is argument handling/parsing, something like this: (first google hit) http://wwwx.cs.unc.edu/~sparkst/howto/cpp_main.php

Ashwani
6th May 2010, 07:37
i dont need to call external c++ program from c++ but i want to create a gui which will write to my program / executable
i know QProcess::start is good enough to start an external executable ... but i dont know how to write the value from radiobutton to my process ...
i m good in c++ i dont need argument handling/parsing but i want to create stand alone or gui to make my program user friendly .... I am in scientific lab now i want that people can use my programe with ease ..
I Need to create a GUI ... thanks

Talei
6th May 2010, 18:46
i know QProcess::start is good enough to start an external executable ... but i dont know how to write the value from radiobutton to my process ...
I read Your posts three times and don't quite get what is Your problem, but if You want to pass arguments to You external program depending on the radiobutton state then do something like this:


if( ui->radioButton->isChecked() )
proc.write("3\n");
if( ui->radioButton2->isChecked() )
proc.write("7\n");
then place this code in i.e. pushbutton, or whatever place You execute You external program, and depending on the radiobutton state these argument will be sent. Also You mentioned:

proc.write("7\n"); // input from a radio button as i have need integer between 1-9 as cut off
I think beter for this purpouse will be QSpinBox (setup it so min val = 1, and max = 9), somthing like this [draft code didn't tested it]:

QString str = QVariant( ui->spinBox->value() ).toString() + "\n";
proc.write( str );

Best regards

Ashwani
7th May 2010, 11:47
QString str = QVariant( ui->spinBox->value() ).toString() + "\n";
proc.write( str );

thanks :cool: Talei thats what i am lookin for ....
Thank you so much

well i want to run an external executable (exe or ./exe) from qt ;) never mind ....

just one more help plz ... how to upload file in qt ....

Talei
7th May 2010, 18:56
You are welcome,
As for the upload, You didn't specified wher to upload. Is it ftp,www with custom php upload script, depending on these the approach is different.
For ftp use QFtp, look i.e. here http://www.qtcentre.org/threads/30502-QFtp-commandFinished%28%29-slot-is-not-called
For WWW QNetworkClasses with reply etc.., if it's custom php script sniff packet header content then prepare appropriate header and send it from Your application.
Best luck

Ashwani
1st July 2010, 10:53
solved i got the correct code ill post the codes hope some one will get benifited by the code

Ashwani
1st July 2010, 10:57
main .cpp

#include <QApplication>

#include "promo.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
myQtApp *dialog = new myQtApp;

dialog->show();
return app.exec();
}




process.pro

HEADERS = promo.h
SOURCES = promo.cpp main.cpp
FORMS = promo.ui

# install
target.path = promo
sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS *.pro
sources.path = .
INSTALLS += target sources

Ashwani
1st July 2010, 10:58
process.h header file


#ifndef MYQTAPP_H
#define MYQTAPP_H

#include "ui_promo.h"


class myQtApp : public QWidget, private Ui::myQtAppDLG
{
Q_OBJECT

public:
myQtApp(QWidget *parent = 0);


public slots:
void getPath();
void run();
void about();

};


#endif

Ashwani
1st July 2010, 10:59
proocess.cpp main cpp file



#include <QtGui>
#include "promo.h"



myQtApp::myQtApp(QWidget *parent)
{
setupUi(this); // this sets up GUI

// signals/slots mechanism in action
connect( pushButton_browse, SIGNAL( clicked() ), this, SLOT( getPath() ) );
connect( pushButton_run, SIGNAL( clicked() ), this, SLOT( run() ) );
connect( pushButton_about, SIGNAL( clicked() ), this, SLOT( about() ) );
}

QString path;
void myQtApp::getPath() //browse button
{


path = QFileDialog::getOpenFileName(
this,
"Choose a file to open",
QString::null,
QString::null);
lineEdit->setText( path );
}

void myQtApp::run() //to run process
{

int value1;
QString str;
QString combo;


QProcess promo;
promo.start("a.exe"); //my c++ compiled executable
promo.waitForStarted();


if(radioButton->isChecked()) //writing radio button values
promo.write("1\n");
if(radioButton_2->isChecked())
promo.write("2\n");
if(radioButton_3->isChecked())
promo.write("3\n");
if(radioButton_4->isChecked())
promo.write("4\n");

QString num = QVariant(spinBox1->value()).toString()+"\n"; //spin box value to string called num
promo.write(num.toAscii().data()); //writing to QBytearrya ie convert from string to array
promo.write(path.toAscii().data());


promo.closeWriteChannel(); //closing write channel
promo.waitForFinished();

}




void myQtApp::about() //about button
{
QMessageBox::about(this,"About Promotif",
"is running\n"
);
}




Thanks to http://sector.ynet.sk/qt4-tutorial/ and Talei :) thanks