Results 1 to 4 of 4

Thread: Sending command to terminal

  1. #1
    Join Date
    Apr 2016
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Sending command to terminal

    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. 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:
    1. //Sending the command to terminal
    2. QProcess proc;
    3. proc.start("/usr/bin/gnome-terminal");
    4.  
    5. proc.waitForStarted();
    6. proc.write("su");
    7. proc.write("ite");
    8. //proc.write(command);
    9. proc.waitForFinished();
    10.  
    11. QByteArray output = proc.readAll();
    12. proc.close();
    13. ui->dgvResults->setText(output);
    To copy to clipboard, switch view to plain text mode 

    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 ";

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Sending command to terminal

    Why do you need the terminal?
    Why not just execute the command?

    Cheers,
    _

  3. #3
    Join Date
    Apr 2016
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Sending command to terminal

    I get an error when I try to execute with out starting a process so I was using terminal in hopes of seeing the commands working somewhere. The error is QIODevice::read (QProcess): device not open. So what I think I really want to do is start bash and I tried running it in execute and it seems like it is running, at least it waits a few seconds like it is processing before it continues, but I can not fill the display with the results of the command or be certain this is working. If I alter the display command ui->dgvResults->setText(output); to "test" it displays.

    Qt Code:
    1. QProcess proc;
    2. proc.start("/bin/bash");
    3.  
    4. proc.waitForStarted();
    5. proc.execute(command);
    6. proc.waitForFinished();
    7.  
    8.  
    9. QByteArray output = proc.readAll();
    10. ui->dgvResults->setText(output);
    11. proc.close();
    To copy to clipboard, switch view to plain text mode 


    Added after 27 minutes:


    I just want to let everyone know that i solved this with this line of code

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

    proc.waitForFinished();

    QByteArray output = proc.readAll();
    proc.close();
    ui->dgvResults->setText(output);
    Last edited by dcasey627; 3rd April 2016 at 23:17.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Sending command to terminal

    You could also just run the command itself, no need to go through a terminal or bash.

    Cheers,
    _

Similar Threads

  1. Execute command without terminal window
    By RolandHughes in forum Qt Programming
    Replies: 2
    Last Post: 17th June 2015, 10:38
  2. Replies: 12
    Last Post: 19th July 2012, 22:22
  3. Replies: 0
    Last Post: 17th August 2010, 13:37
  4. Replies: 7
    Last Post: 15th April 2007, 02:39
  5. call bash/terminal command with QT
    By myta212 in forum Newbie
    Replies: 1
    Last Post: 13th November 2006, 16:38

Tags for this Thread

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.