Results 1 to 7 of 7

Thread: how to run unix commands in qt creator in qt4.0

  1. #1
    Join Date
    Feb 2012
    Posts
    44
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Question how to run unix commands in qt creator in qt4.0

    in qt3.3 i used to append QString directly to system command but in qt4.0 it is saying can't convert from QString to const char*
    Example in QT 3.3
    QString str ="ls -ltr";
    system(str); // it is working in qt3.3, when i am running same statement in qt creator it giving can't convert from QString to const char*
    any help???

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: how to run unix commands in qt creator in qt4.0

    Well, have you tried to convert QString to const char* yourself and then pass it to the function?

  3. #3
    Join Date
    Feb 2012
    Posts
    44
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: how to run unix commands in qt creator in qt4.0

    i tried that to its not wrking

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: how to run unix commands in qt creator in qt4.0

    So what have you tried? On my side manual conversion just works perfect.

  5. #5
    Join Date
    Feb 2012
    Posts
    44
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: how to run unix commands in qt creator in qt4.0

    QString str = "ls -ltr";
    const char *cWrd = str;
    system(cWrd);

    ERROR::
    error: cannot convert ‘QString’ to ‘const char*’ in initialization


    Added after 9 minutes:


    ok thanks for ur interest i found how to convert

    QString str= "ls -ltr";
    const char *c = str.toLocal8Bit().data();
    Last edited by narlapavan; 6th February 2012 at 13:45.

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: how to run unix commands in qt creator in qt4.0

    Yes, that's the function you need. But this can crash (not likely but possible) because toLocal8Bit() creates a temporary byte array. So better write:
    Qt Code:
    1. QString str= "ls -ltr";
    2. QByteArray ba = str.toLocal8Bit();
    3. system(ba.data());
    To copy to clipboard, switch view to plain text mode 

  7. #7
    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 run unix commands in qt creator in qt4.0

    Or just:
    Qt Code:
    1. system("ls -ltr");
    To copy to clipboard, switch view to plain text mode 

    or:
    Qt Code:
    1. QByteArray ba("ls -ltr");
    2. system(ba.constData());
    To copy to clipboard, switch view to plain text mode 

    There is no point in going through Unicode (which is what QString uses internally) if you don't need it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QExtSerialPort with AT commands
    By Althor in forum Qt Programming
    Replies: 2
    Last Post: 6th September 2011, 18:15
  2. QMake: using unix backquote commands
    By jepessen in forum Newbie
    Replies: 2
    Last Post: 6th May 2011, 11:32
  3. Executing AT commands
    By jay in forum Qt Programming
    Replies: 4
    Last Post: 8th December 2009, 09:49
  4. Mac Commands
    By shyam prasad in forum Qt Programming
    Replies: 1
    Last Post: 9th January 2007, 15:30
  5. Replies: 2
    Last Post: 31st January 2006, 14:36

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.