Results 1 to 5 of 5

Thread: how to use cat command in QProcess

  1. #1
    Join Date
    Jan 2006
    Posts
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default 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
    Qt Code:
    1. void RptPrintBarCodeDlg::pbnOk_clicked()
    2. {
    3. QString line;
    4. line="hello world";
    5. QFile file("print.txt");
    6. if ( file.open( IO_WriteOnly ) )
    7. {
    8. QTextStream stream( &file );
    9. stream<<line;
    10. file.close();
    11. QProcess *printbarcode = new QProcess(this);
    12. printbarcode->setWorkingDirectory(QDir::current());
    13. printbarcode->addArgument("cat");
    14. printbarcode->addArgument("print.txt");
    15. printbarcode->addArgument(">");
    16. printbarcode->addArgument("/dev/lp0");
    17.  
    18. if(!printbarcode->start())
    19. {
    20. QMessageBox::information(this,"Error", "process not started" ,"Ok");
    21. }
    22. }
    23. else
    24. {
    25. QMessageBox::information(this,"Error", "File is not opened" ,"Ok");
    26.  
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 
    can anybody help me

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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?

    Qt Code:
    1. QUrlOperator *op = new QUrlOperator();
    2. op->copy( "print.txt", "/dev/lp0" );
    To copy to clipboard, switch view to plain text mode 

    or

    Qt Code:
    1. QFile fr("print.txt");
    2. QFile pr("/dev/lp0");
    3. if(fr.open(IO_ReadOnly) && pr.open(IO_WriteOnly)){
    4. while(!fr.atEnd()){
    5. pr.putch(fr.getch()); // this is so crude!
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to use cat command in QProcess

    what about using system(const char *) ???
    Current Qt projects : QCodeEdit, RotiDeCode

  4. #4
    Join Date
    Jan 2006
    Location
    Athens - Greece
    Posts
    219
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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
    Qt Code:
    1. QUrlOperator *op = new QUrlOperator();
    2. op->copy( "print.txt", "/dev/lp0" );
    To copy to clipboard, switch view to plain text mode 
    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

  5. #5

    Default 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 needs
    I am sailing with the cane...

Similar Threads

  1. Detect First QProcess finished in a group and kill other
    By Davidaino in forum Qt Programming
    Replies: 3
    Last Post: 11th July 2008, 12:53
  2. QProcess exitStatus()
    By user_mail07 in forum Qt Programming
    Replies: 2
    Last Post: 12th June 2008, 20:51
  3. QProcess and Pipes
    By KaptainKarl in forum Qt Programming
    Replies: 1
    Last Post: 9th April 2007, 23:11
  4. QProcess extremely slow on Windows?
    By Pepe in forum Qt Programming
    Replies: 2
    Last Post: 26th March 2007, 00:25
  5. problem with qprocess
    By deekayt in forum Qt Programming
    Replies: 2
    Last Post: 13th June 2006, 13:30

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.