| Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date | |
| All threads index page 5 | |
Hi,
I am trying to write to stdin of my program using write(). When I start my
program my command line, (obviously)I don't see a problem. When starting the
program using QProcess (in my UI), though the return value of write() is the
length of the string (and not -1), the stdin is not received by my program.
The code is something like:
...
QProcess *myProcess = new QProcess(this);
myProcess->start(program, arguments);
if (!myProcess->waitForStarted())
return false;
quint64 ret = myProcess->write("Test String");
...
The ret value is 11. But my "program" doesn't receive anything. And I know
for sure that the program is running (from my log file).
Am I missing something?
I am using QT 4.3 on Win XP.
Thanks,
Andy
On Thursday 24 April 2008 15:29:50 Andy Gynn wrote:
> QProcess *myProcess = new QProcess(this);
> myProcess->start(program, arguments);
> if (!myProcess->waitForStarted())
> Â Â return false;
> quint64 ret = myProcess->write("Test String");
> ...
>
> The ret value is 11. But my "program" doesn't receive anything. And I know
> for sure that the program is running (from my log file).
>
> Am I missing something?
Yes. You have to call waitForBytesWritten().
QProcess is asynchronous, like QTcpSocket. What you write with the
QProcess::write() function is only queued and will be sent later, whenever Qt
is able to.
--
[ signature omitted ]
Attachment:
signature.asc
Description: This is a digitally signed message part.
I did wait for some time (order of tens of seconds). If this is the problem,
is there a way to flush or get an indication from QT that the queue was
emptied?
Thanks,
Andy
On Thu, Apr 24, 2008 at 10:04 AM, Thiago Macieira <
thiago.macieira@xxxxxxxxxxxxx> wrote:
> On Thursday 24 April 2008 15:29:50 Andy Gynn wrote:
> > QProcess *myProcess = new QProcess(this);
> > myProcess->start(program, arguments);
> > if (!myProcess->waitForStarted())
> > return false;
> > quint64 ret = myProcess->write("Test String");
> > ...
> >
> > The ret value is 11. But my "program" doesn't receive anything. And I
> know
> > for sure that the program is running (from my log file).
> >
> > Am I missing something?
>
> Yes. You have to call waitForBytesWritten().
>
> QProcess is asynchronous, like QTcpSocket. What you write with the
> QProcess::write() function is only queued and will be sent later, whenever
> Qt
> is able to.
>
> --
> Thiago José Macieira - thiago.macieira AT trolltech.com
> Trolltech ASA - Sandakerveien 116, NO-0402 Oslo, Norway
>
On Thursday 24 April 2008 17:25:07 Andy Gynn wrote: > I did wait for some time (order of tens of seconds). If this is the > problem, is there a way to flush or get an indication from QT that the > queue was emptied? to flush, call waitForBytesWritten(). The indication is the bytesWritten(qint64) signal. -- [ signature omitted ]
Attachment:
signature.asc
Description: This is a digitally signed message part.
Hi, I tried bytesWritten(qint64). The signal is not coming but the my program (in the process) is not receiving it. Is there a way I can debug this? Thanks, Andy On Thu, Apr 24, 2008 at 11:48 AM, Thiago Macieira < thiago.macieira@xxxxxxxxxxxxx> wrote: > On Thursday 24 April 2008 17:25:07 Andy Gynn wrote: > > I did wait for some time (order of tens of seconds). If this is the > > problem, is there a way to flush or get an indication from QT that the > > queue was emptied? > > to flush, call waitForBytesWritten(). The indication is the > bytesWritten(qint64) signal. > > -- > Thiago José Macieira - thiago.macieira AT trolltech.com > Trolltech ASA - Sandakerveien 116, NO-0402 Oslo, Norway >
On 24 Apr 2008, at 5:52 pm, Andy Gynn wrote: > Hi, > > I tried bytesWritten(qint64). The signal is not coming but the my > program (in the process) is not receiving it. Is there a way I can > debug this? > Could it be that your program (or the library routine it uses to read input) is doing line-buffered input, so it's waiting for a newline character? JK > Thanks, > Andy > > On Thu, Apr 24, 2008 at 11:48 AM, Thiago Macieira > <thiago.macieira@xxxxxxxxxxxxx> wrote: > On Thursday 24 April 2008 17:25:07 Andy Gynn wrote: > > I did wait for some time (order of tens of seconds). If this is the > > problem, is there a way to flush or get an indication from QT > that the > > queue was emptied? > > to flush, call waitForBytesWritten(). The indication is the > bytesWritten(qint64) signal. > > -- > Thiago José Macieira - thiago.macieira AT trolltech.com > Trolltech ASA - Sandakerveien 116, NO-0402 Oslo, Norway >
To rule out other issues, I used a small test code:
#include <stdio.h>
int main (void)
{
char buffer[128];
while(1)
{
fgets(buffer,128,stdin);
fprintf(stderr,"output: %s\n",buffer);
fflush(stdin);
}
}
This code writes whatever it gets for input. Here also the behaviour is
exactly the same as my program. When I run this sample code stand alone, its
runs fine. When I start it from QProcess, there is no input/output.
Can you suggest what might be wring?
Thanks,
Andy
On Thu, Apr 24, 2008 at 2:04 PM, Jonathan Kew <jonathan_kew@xxxxxxx> wrote:
>
> On 24 Apr 2008, at 5:52 pm, Andy Gynn wrote:
>
> Hi,
>
> I tried bytesWritten(qint64). The signal is not coming but the my program
> (in the process) is not receiving it. Is there a way I can debug this?
>
>
> Could it be that your program (or the library routine it uses to read
> input) is doing line-buffered input, so it's waiting for a newline
> character?
>
> JK
>
> Thanks,
> Andy
>
> On Thu, Apr 24, 2008 at 11:48 AM, Thiago Macieira <
> thiago.macieira@xxxxxxxxxxxxx> wrote:
>
>> On Thursday 24 April 2008 17:25:07 Andy Gynn wrote:
>> > I did wait for some time (order of tens of seconds). If this is the
>> > problem, is there a way to flush or get an indication from QT that the
>> > queue was emptied?
>>
>> to flush, call waitForBytesWritten(). The indication is the
>> bytesWritten(qint64) signal.
>>
>> --
>> Thiago José Macieira - thiago.macieira AT trolltech.com
>> Trolltech ASA - Sandakerveien 116, NO-0402 Oslo, Norway
>>
>
>
>
Andy Gynn wrote: >Can you suggest what might be wring? Please post a sample of your own code. -- [ signature omitted ]
Attachment:
signature.asc
Description: This is a digitally signed message part.
Its there in my first post of the topic. I am posting it again:
QProcess *myProcess = new QProcess(this);
myProcess->start(program, arguments);
if (!myProcess->waitForStarted())
return false;
quint64 ret = myProcess->write("Test String");
...
On Thu, Apr 24, 2008 at 4:48 PM, Thiago Macieira <
thiago.macieira@xxxxxxxxxxxxx> wrote:
> Andy Gynn wrote:
> >Can you suggest what might be wring?
>
> Please post a sample of your own code.
> --
> Thiago José Macieira - thiago.macieira AT trolltech.com
> Trolltech ASA - Sandakerveien 116, NO-0402 Oslo, Norway
>
Andy Gynn wrote:
>Its there in my first post of the topic. I am posting it again:
>
>QProcess *myProcess = new QProcess(this);
>myProcess->start(program, arguments);
>if (!myProcess->waitForStarted())
> return false;
>quint64 ret = myProcess->write("Test String");
This one is missing the call to waitForBytesWritten that I told you in the
second post of the topic.
--
[ signature omitted ]
Attachment:
signature.asc
Description: This is a digitally signed message part.
On 24 Apr 2008, at 8:25 pm, Andy Gynn wrote:
> To rule out other issues, I used a small test code:
>
> #include <stdio.h>
>
> int main (void)
> {
> char buffer[128];
> while(1)
> {
> fgets(buffer,128,stdin);
> fprintf(stderr,"output: %s\n",buffer);
> fflush(stdin);
fflush(stdin) here is meaningless (and should be setting an error
code, though you're not checking it).
>
> }
> }
>
>
> This code writes whatever it gets for input. Here also the
> behaviour is exactly the same as my program. When I run this sample
> code stand alone, its runs fine. When I start it from QProcess,
> there is no input/output.
>
> Can you suggest what might be wring?
I already did. Your calling code didn't put a newline at the end of
the string; in the child process, fgets() doesn't return until it
finds newline or EOF. (And it wouldn't get EOF until your QProcess is
destroyed, at which point the child process gets terminated, so then
it's too late.)
JK
>
>
> Thanks,
> Andy
> On Thu, Apr 24, 2008 at 2:04 PM, Jonathan Kew
> <jonathan_kew@xxxxxxx> wrote:
>
> On 24 Apr 2008, at 5:52 pm, Andy Gynn wrote:
>
>> Hi,
>>
>> I tried bytesWritten(qint64). The signal is not coming but the my
>> program (in the process) is not receiving it. Is there a way I can
>> debug this?
>>
>
> Could it be that your program (or the library routine it uses to
> read input) is doing line-buffered input, so it's waiting for a
> newline character?
>
> JK
>
>> Thanks,
>> Andy
>>
>> On Thu, Apr 24, 2008 at 11:48 AM, Thiago Macieira
>> <thiago.macieira@xxxxxxxxxxxxx> wrote:
>> On Thursday 24 April 2008 17:25:07 Andy Gynn wrote:
>> > I did wait for some time (order of tens of seconds). If this is the
>> > problem, is there a way to flush or get an indication from QT
>> that the
>> > queue was emptied?
>>
>> to flush, call waitForBytesWritten(). The indication is the
>> bytesWritten(qint64) signal.
>>
>> --
>> Thiago José Macieira - thiago.macieira AT trolltech.com
>> Trolltech ASA - Sandakerveien 116, NO-0402 Oslo, Norway
>>
>
>