[Qt-interest] newbie: application locks up
john doe
thebiggestbangtheory at gmail.com
Fri May 1 23:56:37 CEST 2009
On Fri, May 1, 2009 at 1:12 PM, john doe <thebiggestbangtheory at gmail.com>wrote:
>
>
> On Fri, May 1, 2009 at 2:47 AM, chandrasekar wagmare <
> sekarwagmare at gmail.com> wrote:
>
>>
>> use this two signals in QProcess() :
>> void readyReadStandardOutput(); //when a stdoutput is available
>> void readyReadStandardError(); //when a stderror is available
>>
>> and use QByteArray readAllStandardOutput(); and readAllStandardError() ;
>> to read the std output or error of the external process in a QString and
>> display it in the box;
>>
>>
> This is great! thanks a ton!
>
>
>>
>> ay 1, 2009 at 1:19 PM, john doe <thebiggestbangtheory at gmail.com> wrote:
>>
>>>
>>>
>>> On Fri, May 1, 2009 at 12:46 AM, chandrasekar wagmare <
>>> sekarwagmare at gmail.com> wrote:
>>>
>>>>
>>>> u have two ways, invoking an external program using QProcess()
>>>> and if u dont want the gui part freeze use QThread() .
>>>>
>>>
>>> awesome! I will look them up.. and since we are on the topic how can I
>>> set up a 2-way communication with the external program to display any error
>>> msgs in a text box or something?
>>>
>>> Thanks again
>>>
>>>
>>>> On Fri, May 1, 2009 at 1:08 PM, john doe <
>>>> thebiggestbangtheory at gmail.com> wrote:
>>>>
>>>>> Hello all,
>>>>> Still relative newbie with Qt. I have made my first form
>>>>> containing combo buttons, lineedits and whatnot :-). I am facing a problem
>>>>> though regarding calling an external program from within the C++ Qt code.
>>>>>
>>>>> I have a ubuntu system and want to invoke a shell script which does
>>>>> some string manipulations on a large file. I used system() within the C++
>>>>> code to do it and it runs. The problem is that when I press a button to
>>>>> start the external program, if it takes 10 minutes to finish, the button
>>>>> seems to be depressed for this duraion. Basically, like waiting for the
>>>>> program to finish. How can I make the button not be depressed for this
>>>>> complete duration so that I can possibly start another instance of the
>>>>> external program by pressing a button on the main form.
>>>>>
>>>>> I am not an expert programmer, but I think this smells of asynchronous
>>>>> calls..right? Any insight would be greatly appreciated :-).
>>>>>
>>>>> I did try using a & at the end of the external program I am invoking
>>>>> but that does not help. Nothing runs. I am using kdesudo to start the
>>>>> external program as it requires certain privileges.
>>>>>
>>>>> Also I tried to use a progress bar with minimum and max set to 0 to
>>>>> continuosly work while the external program is running and it does not seem
>>>>> to work. Any pointers to threads/articles?
>>>>>
>>>>> Thanks in advance.
>>>>>
>>>>> _______________________________________________
>>>>> Qt-interest mailing list
>>>>> Qt-interest at trolltech.com
>>>>> http://lists.trolltech.com/mailman/listinfo/qt-interest
>>>>>
>>>>>
>>>>
>>>>
>>>> --
>>>> WAGMARE
>>>>
>>>
>>>
>>
>>
>> --
>> WAGMARE
>>
>
> Thanks everyone for helping out. I used the suggestions to write up some
code..which unfortunately does not work.. its probably some easy thing that
I am not identifying!
I am using Qt 4.4.3, on Ubuntu 8.10 and am using QDevelop as an IDE. I have
3 files dialogimpl.h, min.cpp and dialogimpl.cpp. The code follows below.
dialogimpl.h
[code]
#ifndef DIALOGIMPL_H
#define DIALOGIMPL_H
//
#include <QDialog>
#include "ui_dialog.h"
//
class DialogImpl : public QDialog, public Ui::mygui
{
Q_OBJECT
public:
DialogImpl( QWidget * parent = 0, Qt::WFlags f = 0 );
public slots:
void getPathdir();
void getPathsisterfile();
void startjob();
void readstdoutput();
};
#endif
[/code]
main.cpp contains
[code]
#include <QApplication>
#include "dialogimpl.h"
//
int main(int argc, char ** argv)
{
QApplication app( argc, argv );
DialogImpl win;
win.show();
app.connect( &app, SIGNAL( lastWindowClosed() ), &app, SLOT( quit() ) );
return app.exec();
}
[/code]
and finally dialogimpl.cpp contains
[code]
#include <QtGui>
#include "dialogimpl.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <QThread>
using namespace std;
//code still being tested
QObject *parent;
QString program;
QProcess *myProcess = new QProcess(parent);
QStringList arguments;
//
DialogImpl::DialogImpl( QWidget * parent, Qt::WFlags f)
: QDialog(parent, f)
{
setupUi(this);
// signals/slots mechanism in action
connect( startjobpushButton, SIGNAL( clicked() ), this, SLOT( startjob() )
);
connect( pushButton_browse_1, SIGNAL( clicked() ), this, SLOT( getPathdir()
) );
connect( pushButton_browse_2, SIGNAL( clicked() ), this, SLOT(
getPathsisterfile() ) );
connect(myProcess, SIGNAL(readyReadStandardOutput()), this,
SLOT(readstdoutput()));
}
//This function makes does something
void DialogImpl::startjob()
{
//declare vars
....
//make a config file
QFile file("/home/temporary-file.txt");
QString line;
if ( file.open(QIODevice::WriteOnly) ) {
// file opened successfully
// use a text stream
QTextStream out( &file );
out << "Target=" << Target << endl;
// Close the file
file.close();
//start the job
program="echo";
arguments << "hello";
myProcess->start(program, arguments);
//remove the file
file.remove();
}
else
{
//failed to open file
textEdit_output->insertPlainText("ERROR: Failed to create temporary file");
}
}
//This function allows for the user to
//read std output
void DialogImpl::readstdoutput()
{
QString output;
output=myProcess->readAllStandardOutput();
textEdit_output->insertPlainText("ERROR: test msg to be displayed in text
box");
textEdit_output->append(output);
}
[/code]
What I want to do is when I click startjobpushbutton, a process should be
invoked, which does "echo hello" and I should be able to see "hello" in the
textEdit_ouput text box. Currently, when I click the button nothing happens.
Can someone please provide a little insight.
Thanks in advance.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090501/d6cf3138/attachment.html
More information about the Qt-interest-old
mailing list