- 1. widget.h
- 2. widget.cpp
With the QProcess class, you can create your own command-line console. To do this, just create the application with a GUI and mark the QLineEdit and QTextEdit (lineEdit and textEdit) objects on the main window to write a query and display the query result. Add to our class an object of type QProcess. It is necessary to execute system commands. Create two slots. A command () slot for executing a command prompt. This slot we connect to the returnPressed () signal of the lineEdit object. The slot will be called when you press the "Enter" key, when the focus is our text line. When executed, the text from lineEdit is read and passed as an argument to the start () function of the QProcess class object. To work on Windows, you need to add the prefix "cmd / C" to the command line. The setStdout () slot will receive the data stream received from the QProcess class object and display it in textEdit. Here it is necessary to consider one more feature of Windows is that the encodings on the command line and the OS itself are different. Conversion can be done using the toUnicode () method of the QTextCodec class. In this case, you need to set the encoding "IBM 866".
widget.h
#ifndef WIDGET_H #define WIDGET_H #include <QtCore> #include <QWidget> namespace Ui { class Widget; } class Widget : public QWidget { Q_OBJECT public: explicit Widget(QWidget *parent = 0); ~Widget(); public slots: void setStdout(); void command(); private: Ui::Widget *ui; QProcess *m_process; }; #endif // WIDGET_H
widget.cpp
#include "widget.h" #include "ui_widget.h" Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); m_process = new QProcess(this); connect(m_process, SIGNAL(readyReadStandardOutput()), this, SLOT(setStdout()) ); connect(ui->lineEdit, SIGNAL(returnPressed()), this, SLOT(command()) ); } Widget::~Widget() { delete ui; } void Widget::setStdout() { if(QSysInfo::productType()=="windows") { QTextCodec *codec = QTextCodec::codecForName("IBM 866"); ui->textEdit->append( codec->toUnicode(m_process->readAllStandardOutput())); } else ui->textEdit->append( m_process->readAllStandardOutput() ); } void Widget::command() { QString strCommand; if(QSysInfo::productType()=="windows") strCommand = "cmd /C "; strCommand += ui->lineEdit->text(); m_process->start(strCommand); }
Work of the program
The figure below shows the output of the dir command:
With this program, you can implement the management of the operating system without the use of API-functions. It should be remembered that all the commands of this program are executed from the project assembly directory. Use the "cd" command to change the directory is not possible, because Each command is executed in a separate session and any transition will be reset.
Can you please upload the ui_widget.h so this can compile in total?
ui_widget.h should be created during the first time of build project. ui_****.h files commonly were created from *.ui files. Therefore, just build project in Qt Creator.
Добрый вечер. Как решить проблему с двойными кавычками. Например для команды arp -a | find /i "c8-cb-b8-63-41-60" ? Пробовал разное экранирование, но что то ничего найти не могу.
А разве обычный вариант с экранированием вовсе не работает?
Нет.
Возможно проблема в самой команде find? Она почему-то не возвращает ни каких данных
может быть у вас не совсем верно указан путь к директории. Если я правильно понимаю, то /i - это директория? , если это относительный путь, то надо так писать
Большое спасибо за данную статью
вы решили мою проблему с кодировкой символов)