QProcess::Write()::issue



  • Hey guys! I have a question here. There is a line edit box and push button in my program. Supposedly,the line edit box is let the let user to type text, and the program must be able to get the text from what the user had typed , and then perform a following function unto it . such function are show in below code.

    void MainWindow::on_pushButton_3_clicked()
    {
        QString message A= "mkdir ";
        QString messageB = ui->lineEdit->text();
        messageA.append(messageB);
    
    
        QProcess procB;
        procB.start("cmd");
        if(!procB.waitForStarted()){
            return;
        }
     
        procB.write(messageA);
        QByteArray outputB;
        if(procB.waitForReadyRead()){
            outputB += procB.readAll();
        }
       qDebug()<<outputB;
    
    }
    

    I keep getting this error when i try to compile the codes, the error lies at the "procB.write(messageA)". The further explanation of the error is cannot convert arguement 1 from QString to const char. i know this cannot be done according to the Documentation. If there is another alternative solution that more or less serving the same purpose as my function, then please share it with me. Your help will be appreciated !


  • Moderators

    @QT_QT_QT
    In your post it says:

        QString message A= "mkdir ";
    

    This hsould give already a compile error.



  • HI

    its just a typo when i copy and paste the code. my apologies.


  • Qt Champions 2016

    something like
    procB.write( messageA.toStdString().c_str() );


  • Qt Champions 2016

    @mrjj @QT_QT_QT
    Or possibly (which does seem simpler to me):

    static const char * const messageA = "mkdir";
    procB.write(messageA);
    

    If the QString is actually coming from another part of the program and is not a constant (as in your case), you can also use qPrintable:

    QString messageA("mkdir");
    procB.write(qPrintable(messageA));
    

  • Lifetime Qt Champion

    Hi,

    You can also just use QByteArray.

    QByteArray messageA("mkdir");
    procB.write(messageA);
    

    and if it comes for whatever reason from a QString.

    QString messageA("mkdir");
    procB.write(messageA.toLatin1());
    

    Unless you are on a system that uses an encoding not matching Latin1. In that case you can replace toLatin1 with toUtf8.



  • Greetings to @mrjj & @kshegunov

    @mrjj said:

    procB.write( messageA.toStdString().c_str() );

    Thank you for providing solution over here. mrjj's solution works for me ! thank you very much!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.