PDA

View Full Version : Need help!!!!! Problem in running a program with the help of QProcess



aaditya190
5th December 2013, 10:17
I am making a qt gui application in which i have a textedit and some other gui components. I wish to link the textedit to the compiler.. Is this possible? If yes, can anyone suggest me the way to do so? Any help would be appreciable.:)

zaphod.b
5th December 2013, 13:11
Do you wish to display compiler output inside a QTextEdit? Does your app start the compiler process? In this case have a look at QProcess::readyReadStandardOutput().

aaditya190
6th December 2013, 05:15
Yes i wish to pick the source code from one textedit and display its output in another textedit in the same application. Is there some way to implement this?

ChrisW67
6th December 2013, 06:30
Yes i wish to pick the source code from one textedit and display its output in another textedit in the same application.
Nothing to do with "Linking" or "to the compiler", just "textedit". I assume you mean a QTextEdit.


Is there some way to implement this?
Yes, copying the "source code", if that is what is in the text edit, to another text edit is easy.

QTextEdit::toHtml() and QTextEdit::setHtml()
or
QTextEdit::toPlainText() and QTextEdit::setPlainText()
or
QTextEdit::document(), QTextDocument::clone(), and QTextEdit::setDocument()


There is a small thing in every copy of Qt called Assistant. Assistant contains overview, reference documentation and examples you might find informative. Assistant is not a substitute for a basic knowledge of the programming language, compiler and other tools you are using.

aaditya190
6th December 2013, 06:46
I guess I couldn't explain my problem correctly. I have a textedit. I want that whatever text is written in the textedit, on the click of a button or an icon, that text goes to the compiler and compiler returns the suitable output. Is there any way to do so that I can have an interactive application just like any other Integrated Development Environment(IDE) ? Any help would be highly appreciable..:)

ChrisW67
6th December 2013, 06:48
Zaphod.b has already told you the tools you need to do that.

aaditya190
12th December 2013, 10:08
I have a gui application containing 2 plain text edits. Following is the image....9848..

I wish to implement a concept that the code that I write in Plain Text Edit 1, its output is displayed in Plain Text Edit 2 as mentioned in the image i.e linking the plaintextedit1 to the gcc compiler... Is this possible in qt? If yes, can anyone help me out with this??


e.g. 9849..in which plaintextedit2 is displaying the output..

Is this possible in qt?

Radek
12th December 2013, 12:00
If I understand well, you are creating an IDE. Note that the output on your pictures is not a result of mere passing a source to GCC. The compiled source needs to be linked and then run to produce some output. Therefore, you need to create a makefile and pass the makefile to make.

A memdisk would come handy here but AFAIK Qt does not offer a memdisk. Therefore have a directory for your app and when you click the button in text edit 1:

(1) Delete the contents of the directory.
(2) Store the source file in the directory.
(3) Create a makefile and store it in the directory, too.
(4) Run make in the directory.
(5) Pipe stdout and stderr to text edit 2.
(6) Run the app. Have some "kill me" mechanism implemented.

If the source file is always only one then the source file can be stored with some "standard name" so that the makefile will be always the same. This simplifies (1) - (6) considerably.

aaditya190
13th January 2014, 09:30
@Chrisw67 and @Stampede... I tried the code in the QProcess thread but I am unable to understand that How can I link it to the compiler? Can anyone of you please explain me stepwise as to how this can be done?? Any kind of help will be highly appreciated..

parulkalra14
14th January 2014, 10:41
@Radek : Thanku so much for this answer. Sir I am new to Qt Creator Can you please ellaborate these steps so that these will be more clear?

parulkalra14
4th February 2014, 06:12
Need Help!!!!!!!

Sir i followed these steps and make a file which reads program written on plainTextEdit and batch file for output and i am able to reterive the output from from batch file and display it to another plainTextEdit but problem arises in case of cin and scanf statements .For example :


#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"Enter two numbers";
cin>>a>>b;
return 0;
}

When i compiled this program a batch file will open that does not contain Enter two numbers statement that means it can take the input only but does show cout/printf statements,that is creating problem because user will not be able to detect what to enter. As i understands the problem and know what to enter in a batch file suppose i enetered 2 3 so output reterive to a plainTextEdit is correct i.e Enter two numbers 2 3. But problem occurs at User Console/Command Prompt.

anda_skoa
4th February 2014, 09:28
Compilation does not execute a program, it just transforms source code into machine code.

If you want to run an existing program, see QProcess.
If you want to read what such a program writes to its standard output stream, see QProcess::readyReadStandardOutput()

Cheers,
_

parulkalra14
10th February 2014, 09:11
First of all I make a file which reads program written on plainTextEdit and batch file for output and i am able to reterive the output from batch file and display it to another plainTextEdit but problem arises in case of cin and scanf statements .For example :

#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"Enter two numbers";
cin>>a>>b;
return 0;
}
When i compiled this program a batch file will open that does not contain Enter two numbers statement that means it can take the input only but does show cout/printf statements,that is creating problem because user will not be able to detect what to enter. As i understands the problem and know what to enter in a batch file suppose i enetered 2 3 so output reterive to a plainTextEdit is correct i.e Enter two numbers 2 3. But problem occurs at User Console/Command Prompt.

Code is :
bool MainWindow::run_file()
{
QMessageBox msgBox;
QString program = "cons.bat";
QString st="PRESS OKK!!";
QStringList arguments;
QProcess *proc = new QProcess(this);
proc->startDetached(program);

if(proc->NotRunning)
msgBox.setText(st);
msgBox.exec();
proc->waitForFinished();


display_output();
return(true);

}
void MainWindow::display_output()
{

QFile file("out.txt");
if(!file.open(QIODevice::ReadOnly)) {
QMessageBox::information(0, "error", file.errorString());
}
//QTextStream str(&file);

QString out_str;

while (!file.atEnd())
{
out_str=file.readAll();
}
ui->plainTextEdit_3->clear();
ui->plainTextEdit_3->insertPlainText(out_str);
file.remove()
}

In an attached file output.png Enter two numbers is not written. As i know i have to enter two numbers then i entered 2 3 but out.txt contain the whole program output like:
Enter two numbers 2 3

But problem is with console window.

anda_skoa
10th February 2014, 14:05
This continued opening of the same thread over and over again it really getting annoying.
It means one of the admins has to search for the duplicate and manually merge the thread.


When i compiled this program a batch file will open that does not contain Enter two numbers statement

When you compile a program then it is not executed.
Building and running are two very different operations.



Code is :

For the love of $DETIY use code tags.





QProcess *proc = new QProcess(this);
proc->startDetached(program);

if(proc->NotRunning)
msgBox.setText(st);
msgBox.exec();
proc->waitForFinished();


That does not make any sense at all.

QProcess::startDetached() is a static method, "proc" will be totally unaffected.

NotRunning is a state enum value, not a public variable. That condition is always false (NotRunning == 0).

The message box should probably not always be executed.

Cheers,
_