PDA

View Full Version : Shell command from Qt 4.5.2 by QProcess



Rajeshsan
18th December 2009, 06:17
Hi Everyone.........
Plz can you send me small code just it has to send some command to CLI. I'm working in ARM i.MX31 ARM board. I need to interact CLI by using Qt 4.5.2. So i need just a simple code to execute on CLI (command Line Interface). For example i need to see all list of folders or dir on Root file system(rfs). Plz can you check it out, give solution how to do it. If i click on button it has to display list of folders and dir on CLI first.
Im writing my complete code of my project here and im attaching .ui file as in zip format. Plz check it out, assit me to pass commands for CLI. plz quote an small example also.overall if i click on button on GUI. it has to send command "ls -l" to CLI, Atleast i need to list of folders and directories on CLI after cancelling GUI, later on i can work it out to bring uo data on CLI to textedit box.. Thanking you in advance:) see all my attached and written code below.

Asking directly my AIM is to just click on button, if i cancel GUI. atleast i want see "ls -l" cmd on terminal and it should have list all the folders and directories.:)


QPROCESS.PRO
================================================== ============================================
#-------------------------------------------------
#
# Project created by QtCreator 2009-12-10T15:21:05
#
#-------------------------------------------------

TARGET = Qprocess
TEMPLATE = app


SOURCES += main.cpp\
mainwindow.cpp

HEADERS += mainwindow.h

FORMS += mainwindow.ui
================================================== ================================================== ==========

main.cpp
================================================== ================================================== ==========
#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
================================================== ================================================== ===========
mainwindow.cpp
================================================== ================================================== ===========
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);

pushbutton =new QPushButton;
pushbutton=ui->pushButton;

connect(pushbutton,SIGNAL(clicked()),this,SLOT(but tonclicked()));





}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::buttonclicked()
{
pushbutton->setText("VINAY");
proc=new QProcess(this);
proc->start("VKB -qws");//Actually i want to give commands like ls -l or cd .. which should be passed to CLI and act on that. but im not finding options in proc-> . Plz can you send me a small code to interact with CLI.

}
================================================== ================================================== ===========
mainwindow.h
================================================== ================================================== ===========
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>
#include <QProcess>
#include <QPushButton>
#include <QTextEdit>
class QProcess;

namespace Ui
{
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);
~MainWindow();

private:
Ui::MainWindow *ui;
private:
QProcess *proc;
QPushButton *pushbutton;
private slots:
void buttonclicked();
};

#endif // MAINWINDOW_H
================================================== ===================================

Lykurg
18th December 2009, 07:54
Hi Everyone.........
Plz can you send me small code just it has to send some command to CLI.
[...]
proc=new QProcess(this);
proc->start("VKB -qws");//Actually i want to give commands like ls -l or cd .. which should be passed to CLI and act on that. but im not finding options in proc-> . Plz can you send me a small code to interact with CLI.

Before begging for code, I would recommend using the CODE tags and read the docs regarding QProcess and learn how the right syntax would look like.

Rajeshsan
18th December 2009, 08:11
Hi.... I have sent full code. So plz can you see the code then you can suggest me. how to pass arguments for Shell from Qt. I need to develop just a LineEdit. In LineEdit what i type like command "ls -l". It should appear on CLI then it should display the list of Folders and directories. I need just simple application like this or plz send simple code to do it.

squidge
19th December 2009, 23:43
i need to see all list of folders or dir on Root file system(rfs). Use QDir in this case.

If you wish to send other commands, then refer to QProcess documentation. Note that arguments must be seperate from the command to be executed (look at QStringList).

Rajeshsan
21st December 2009, 10:56
Sorry.. Actually I wanted to implement on my board fast. so im searching fastly. I thot of replying you but now im getting partial outputs. So I'm eager to do fast. Now im able to get cmd.exe(wiindows) or terminal.exe(linux).just proc->start("cmd.exe/terminal.exe"). I need to send command to same terminal or command prompt directly from same program or window. I studied full documentation but i didmt suceed in sending commands to it. So I dont no how exactly it happens. I have sent code. QDir i implented as you suggested but it didnt work. Sorry.

squidge
21st December 2009, 16:03
Why not run each command seperately? It would be much easier. For example, 'dir' command can be run like:


QProcess p;
p.start("cmd.exe", QStringList() << "/c" << "dir");
if (p.waitForStarted())
{
p.waitForFinished();
qDebug() << p.readAllStandardOutput();
}