PDA

View Full Version : how to use cat command in QProcess



renjithmamman
27th January 2006, 08:17
i have a file in the project dirictory. i wnt to print it using qprocess

i tried like this but not working

void RptPrintBarCodeDlg::pbnOk_clicked()
{
QString line;
line="hello world";
QFile file("print.txt");
if ( file.open( IO_WriteOnly ) )
{
QTextStream stream( &file );
stream<<line;
file.close();
QProcess *printbarcode = new QProcess(this);
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

wysota
27th January 2006, 09:18
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?


QUrlOperator *op = new QUrlOperator();
op->copy( "print.txt", "/dev/lp0" );

or


QFile fr("print.txt");
QFile pr("/dev/lp0");
if(fr.open(IO_ReadOnly) && pr.open(IO_WriteOnly)){
while(!fr.atEnd()){
pr.putch(fr.getch()); // this is so crude!
}
}

fullmetalcoder
27th January 2006, 14:16
what about using system(const char *) ???

yop
27th January 2006, 19:06
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




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

dec0ding
27th January 2006, 19:46
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