Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

can anybody explain why QStringList() used in below line ?



  • i want to understand below statement

    qProcessSysCmd ->start("bash",QStringList() << "-c" << sCommand.toLatin1().data());

    what this statement do internally ?


  • Moderators

    Apparently, start() requires a QStringList. Instead of creating it earlier, like so:

    QStringList args;
    args.append("-c");
    args.append(sCommand);
    

    the author has decided to create it in-place (good idea, btw, it allows the compiler to optimize it). The syntax is rather old, in post C++11 world we'd rather write it like this:

    qProcessSysCmd ->start("bash", { "-c", sCommand });
    

    EDIT Removed .toLatin1().data() as it is, indeed, a bad idea to do this.


  • Moderators

    Apparently, start() requires a QStringList. Instead of creating it earlier, like so:

    QStringList args;
    args.append("-c");
    args.append(sCommand);
    

    the author has decided to create it in-place (good idea, btw, it allows the compiler to optimize it). The syntax is rather old, in post C++11 world we'd rather write it like this:

    qProcessSysCmd ->start("bash", { "-c", sCommand });
    

    EDIT Removed .toLatin1().data() as it is, indeed, a bad idea to do this.


  • Lifetime Qt Champion

    @sierdzio said in can anybody explain why QStringList() used in below line ?:

    sCommand.toLatin1().data()

    btw: this is useless and even wrong when the command contains non-latin1 characters


Log in to reply