Problem sending command to terminal with QProcess



  • I am currently using Fedora 23 and QT 5.6. I am building a command for Hping3 and trying to send that command to the terminal and receive the results upon a button push. I can get the terminal to open but I can not get it to receive any commands. I am very new to QT so my knowledge is minimal. I learned programming with Visual Basic so I understand logic but this syntax is killing me.

    Qt Code:

    //Sending the command to terminal
        QProcess proc;
        proc.start("/usr/bin/gnome-terminal");
     
        proc.waitForStarted();
        proc.write("su");
        proc.write("ite");
        //proc.write(command);
        proc.waitForFinished();
     
        QByteArray output = proc.readAll();
        proc.close();
        ui->dgvResults->setText(output);
    

    I am trying to display in a QTextBrowser and Commented out that one write command because I am getting an error

    /home/itestudent/SeniorProject/frmmain.cpp:112: error: no matching function for call to 'QProcess::write(QString&)'
    proc.write(command);
    ^

    Command is declared at the top of the button push event
    QString command = "hping3 -S ";

    Any help would be great


  • Qt Champions 2016

    hi and welcome
    the write seems to want a const char * so try with
    p.write(command .toStdString().c_str());

    ( you give it QString& and it dont want a QString)


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Since you only want the result, are you sure you need the gnome-terminal ? Or is to enter su's password ?

    Use QByteArray for command in place of QString.



  • @SGaist I was trying to get into the su because sudo was not working. Now i figured out how to fix sudo so I am trying to send echo ite | sudo -S hping3 -S -c 5 <portNumber> <IPAdress> to the bash. My code waits like it is processing the command but I can not get it to update the text box(dgvResults). If i change the text box(dgvResults) to a simple string it will output that though. The code that I have so far is

    QProcess proc;
    proc.start("/bin/bash");

        proc.waitForStarted();
        proc.execute(command);
        proc.waitForFinished();
    
        QByteArray output = proc.readAll();
        proc.close();
        ui->dgvResults->setText(output);


  • I just want to thank you for the help. I just found another thread that had exactly what I needed. I swear I tried this before and it didn't work but somehow it does now

    QProcess proc;
    proc.start("/bin/bash", QStringList() << "-c" << command);

        proc.waitForFinished();
    
        QByteArray output = proc.readAll();
        proc.close();
        ui->dgvResults->setText(output);
    

    Again thank you for the responses they were very helpful


  • Lifetime Qt Champion

    Glad you found out and thanks for sharing !

    Since you have it working now, please mark the thread as solved using the "Topic Tool" button so that other forum users may know a solution has been found :)



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