0

I need to make a QT GUI application that will be able to run the command line batchs and commands. For example, ping, tcpdump, etc. ...

I would imagine it like this: The standard graphical window with the QTableView, some checkboxes, etc. ... with a component instance QPlainTextEdit. This component (QPlainTextEdit) will act as a command line, that will allow to enter commands and capture their output.

Is such a thing possible? How should this be done?

László Papp
  • 51,870
  • 39
  • 111
  • 135
exo
  • 373
  • 5
  • 22

2 Answers2

1

You can use QProcess for your purpose..

QProcess cmd;
cmd.start("cmd");

More details here..

http://www.qtcentre.org/threads/12757-QProcess-cmd

Digital_Reality
  • 4,488
  • 1
  • 29
  • 31
  • For an application that I have written to process LungCT cases in my research I Spawn an QProcess with a command line with the arguments generated in a QStringList from a few options in a QDialog that generates the command line. – drescherjm Jan 01 '14 at 15:36
  • Thanks, it looks usable. I have yet to solve the asynchronous operation, such as password requirements in net use, ssh, etc.. But I hope it will not be a problem. – exo Jan 01 '14 at 16:10
1

The main idea is to use QProcess for running commands. See the code below for demonstration.

Sync approach

QProcess process;

// If "command" is not in your path,
// use the corresponding relative or absolute path

process.start("command", QStringList()
                      << QString("-arg1")
                      << QString("arg2")
                      << QString("-arg3")
                      << QString("arg4"));

// Wait for it to start
if(!process.waitForStarted())
    return 0;

bool retval = false;
QByteArray buffer;
while ((retval = process.waitForFinished()));
    buffer.append(process.readAll());

if (!retval) {
    yourPlainTextEdit.appendPlainText(process.errorString());
} else {
    yourPlainTextEdit.appendPlainText(buffer);
}

Async approach

MyClass::MyClass(QQProcess *process, QObject *parent)
    : QObject(parent)
    , m_process(process)
{
    connect(m_process, SIGNAL(readyRead()), SLOT(handleReadyRead()));
    connect(m_process, SIGNAL(error(QProcess::ProcessError)), SLOT(handleError(QProcess::ProcessError)));
    connect(&m_timer, SIGNAL(timeout()), SLOT(handleTimeout()));

    m_timer.start(5000);
}

MyClass::~MyClass()
{
}

void MyClass::handleReadyRead()
{
    m_readData.append(m_process->readAll());

    if (!m_timer.isActive())
        m_timer.start(5000);
}

void MyClass::handleTimeout()
{
    if (m_readData.isEmpty()) {
        yourPlainTextEdit.appendPlainText("No data was currently available for reading from gnuplot");
    } else {
        yourPlainTextEdit.appendPlainText("Process successfully run");
    }

}

void GnuPlotReader::handleError(QProcess::ProcessError processError)
{
    if (processError == QProcess::ReadError) {
        appendPlainTextEdit.appendPlainText("An I/O error occurred while reading the data, error: %1").arg(m_process->errorString()));
        yourPlainTextEdit.appendPlainText(m_readData);
    }
}

Disclaimer: This is fully untested code, so it may have compiler and run time issues, but this should give a good grasp of it without further ado.

László Papp
  • 51,870
  • 39
  • 111
  • 135
  • Thank you for code. Catch output and send input from/in text box must be assynchronous operation. Must be in another thread. If you use any command or batch who need user interaction, like yes/no choice. – exo Jan 01 '14 at 16:17
  • @exo: I provided an async approach as well. – László Papp Jan 01 '14 at 16:20
  • I'm sorry. I'm a new on this forum and I do not know all the details. But I needed only help, what direction to go. First answer was OK. I did not expect that someone will supply complet code. I have to do it alone. – exo Jan 02 '14 at 09:45
  • @exo: people usually accept the best answers rather than quickest as per selection etiquette: http://meta.stackexchange.com/questions/19448/etiquette-for-selecting-answers I am not saying mine is better, but I thought I would point this link out, just in case, as you seem to be new to the site (although I am, too). :) – László Papp Jan 02 '14 at 21:11