Qt 5.10 Book Examples
Shell.h
Go to the documentation of this file.
1 // ======================================================================
2 // Shell.h --
3 // ======================================================================
4 // This file is a part of the book
5 // "Qt 5.10 Professional programming with C++"
6 // http://qt-book.com
7 // ======================================================================
8 // Copyright (c) 2017 by Max Schlee
9 // ======================================================================
10 
11 #pragma once
12 
13 #include <QtWidgets>
14 
15 // ======================================================================
16 class Shell : public QWidget {
17 Q_OBJECT
18 private:
19  QProcess* m_process;
20  QLineEdit* m_ptxtCommand;
21  QTextEdit* m_ptxtDisplay;
22 
23 public:
24  // ------------------------------------------------------------------
25  Shell(QWidget* pwgt = 0) : QWidget(pwgt)
26  {
27  m_process = new QProcess(this);
28  m_ptxtDisplay = new QTextEdit;
29 
30  QLabel* plbl = new QLabel("&Command:");
31 
32 #ifdef Q_OS_WIN
33  QString strCommand = "dir";
34 #else
35  QString strCommand = "ls";
36 #endif
37 
38  m_ptxtCommand = new QLineEdit(strCommand);
39  plbl->setBuddy(m_ptxtCommand);
40 
41  QPushButton* pcmd = new QPushButton("&Enter");
42 
43  connect(m_process,
44  SIGNAL(readyReadStandardOutput()),
45  SLOT(slotDataOnStdout())
46  );
47  connect(m_ptxtCommand,
48  SIGNAL(returnPressed()),
49  SLOT(slotReturnPressed())
50  );
51  connect(pcmd, SIGNAL(clicked()), SLOT(slotReturnPressed()));
52 
53  //Layout setup
54  QHBoxLayout* phbxLayout = new QHBoxLayout;
55  phbxLayout->addWidget(plbl);
56  phbxLayout->addWidget(m_ptxtCommand);
57  phbxLayout->addWidget(pcmd);
58 
59  QVBoxLayout* pvbxLayout = new QVBoxLayout;
60  pvbxLayout->addWidget(m_ptxtDisplay);
61  pvbxLayout->addLayout(phbxLayout);
62  setLayout(pvbxLayout);
63  }
64 
65 public slots:
66  // ------------------------------------------------------------------
68  {
69  m_ptxtDisplay->append(m_process->readAllStandardOutput());
70  }
71 
72  // ------------------------------------------------------------------
74  {
75  QString strCommand = "";
76 #ifdef Q_OS_WIN
77  strCommand = "cmd /C ";
78 #endif
79  strCommand += m_ptxtCommand->text();
80  m_process->start(strCommand);
81  }
82 };
83 
84 
void slotReturnPressed()
Definition: Shell.h:73
Shell(QWidget *pwgt=0)
Definition: Shell.h:25
void slotDataOnStdout()
Definition: Shell.h:67
Definition: Shell.h:16