how to use cat command in QProcess
i have a file in the project dirictory. i wnt to print it using qprocess
i tried like this but not working
Code:
void RptPrintBarCodeDlg::pbnOk_clicked()
{
line="hello world";
if ( file.open( IO_WriteOnly ) )
{
stream<<line;
file.close();
printbarcode
->setWorkingDirectory
(QDir::current());
printbarcode->addArgument("cat");
printbarcode->addArgument("print.txt");
printbarcode->addArgument(">");
printbarcode->addArgument("/dev/lp0");
if(!printbarcode->start())
{
QMessageBox::information(this,
"Error",
"process not started" ,
"Ok");
}
}
else
{
QMessageBox::information(this,
"Error",
"File is not opened" ,
"Ok");
}
}
can anybody help me
Re: how to use cat command in QProcess
You can't use ">" and alike in QProcess. They are interpreted by the shell and QProcess doesn't use a shell so all those <, >, |, & etc. are not interpreted.
Can't you just read the file and write it to the printer or even just copy it to the printer directly?
Code:
QUrlOperator *op = new QUrlOperator();
op->copy( "print.txt", "/dev/lp0" );
or
Code:
if(fr.open(IO_ReadOnly) && pr.open(IO_WriteOnly)){
while(!fr.atEnd()){
pr.putch(fr.getch()); // this is so crude!
}
}
Re: how to use cat command in QProcess
what about using system(const char *) ???
Re: how to use cat command in QProcess
Quote:
Originally Posted by fullmetalcoder
what about using system(const char *) ???
Not a good choice IMHO as:
a) You don't control the process
b) The event loop freezes until system(...) returns
Quote:
Originally Posted by wysota
Code:
QUrlOperator *op = new QUrlOperator();
op->copy( "print.txt", "/dev/lp0" );
Wow this is beautiful, I used the second approach (the QFile("/dev/lp0") one) in a seperate thread to send data to a printer (though I used writeBlock()), but this is just beautiful, if I find a chance I'll use it as well
Re: how to use cat command in QProcess
Please note that QProcess does not emulate a shell. This means that QProcess does not do any expansion of arguments: a '*' is passed as a '*' to the program and is not replaced by all the files, a '$HOME' is also passed literally and is not replaced by the environment variable HOME and the special characters for IO redirection ('>', '|', etc.) are also passed literally and do not have the special meaning as they have in a shell.
:) There is QByteStream or any Q*Stream class for those:D needs