Added an example for PyQt5 (#389)
* added pyqt5 example * fixed example path on build * improved pyqt example * simplified example
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
A simple example showing how to use QTermWidget to control and display a remote terminal.
|
||||
|
||||
To run this example, you should:
|
||||
1. Build client-side program. In my PC, I use 'apt-get' to install the QTermWidget library.
|
||||
2. Start the shell-srv.py with specific paramenters.This will expose a shell via socket.
|
||||
3. Start the client-side program from commandline with specific paramenters.
|
||||
|
||||
Now you will get your own remote terminal work with QTermWidget.
|
||||
@@ -0,0 +1,34 @@
|
||||
#-------------------------------------------------
|
||||
#
|
||||
# Project created by QtCreator 2017-10-31T00:37:59
|
||||
#
|
||||
#-------------------------------------------------
|
||||
|
||||
QT += core gui network
|
||||
|
||||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
|
||||
|
||||
TARGET = RemoteTerm
|
||||
TEMPLATE = app
|
||||
|
||||
# The following define makes your compiler emit warnings if you use
|
||||
# any feature of Qt which as been marked as deprecated (the exact warnings
|
||||
# depend on your compiler). Please consult the documentation of the
|
||||
# deprecated API in order to know how to port your code away from it.
|
||||
DEFINES += QT_DEPRECATED_WARNINGS
|
||||
|
||||
# You can also make your code fail to compile if you use deprecated APIs.
|
||||
# In order to do so, uncomment the following line.
|
||||
# You can also select to disable deprecated APIs only up to a certain version of Qt.
|
||||
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
|
||||
|
||||
CONFIG += c++11
|
||||
|
||||
SOURCES += \
|
||||
main.cpp \
|
||||
remoteterm.cpp
|
||||
|
||||
HEADERS += \
|
||||
remoteterm.h
|
||||
|
||||
unix:!macx: LIBS += -lqtermwidget5
|
||||
@@ -0,0 +1,19 @@
|
||||
#include "remoteterm.h"
|
||||
#include <QApplication>
|
||||
#include <QDebug>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
if(a.arguments().size() != 3){
|
||||
qDebug() << "Example(client-side) for remote terminal of QTermWidget.";
|
||||
qDebug() << QString("Usage: %1 ipaddr port").arg(a.arguments()[0]);
|
||||
return 1;
|
||||
}
|
||||
QString ipaddr = a.arguments().at(1);
|
||||
quint16 port = a.arguments().at(2).toUShort();
|
||||
RemoteTerm w(ipaddr,port);
|
||||
w.show();
|
||||
|
||||
return a.exec();
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#include "remoteterm.h"
|
||||
#include <QTcpSocket>
|
||||
#include <QDebug>
|
||||
#include <unistd.h>
|
||||
|
||||
RemoteTerm::RemoteTerm(const QString &ipaddr, quint16 port, QWidget *parent)
|
||||
: QTermWidget(0,parent)
|
||||
{
|
||||
socket = new QTcpSocket(this);
|
||||
|
||||
// Write what we input to remote terminal via socket
|
||||
connect(this, &RemoteTerm::sendData,[this](const char *data, int size){
|
||||
this->socket->write(data, size);
|
||||
});
|
||||
|
||||
// Read anything from remote terminal via socket and show it on widget.
|
||||
connect(socket,&QTcpSocket::readyRead,[this](){
|
||||
QByteArray data = socket->readAll();
|
||||
write(this->getPtySlaveFd(), data.data(), data.size());
|
||||
});
|
||||
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(atError()));
|
||||
|
||||
// Here we start an empty pty.
|
||||
this->startTerminalTeletype();
|
||||
|
||||
socket->connectToHost(ipaddr, port);
|
||||
}
|
||||
|
||||
void RemoteTerm::atError()
|
||||
{
|
||||
qDebug() << socket->errorString();
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#ifndef WIDGET_H
|
||||
#define WIDGET_H
|
||||
|
||||
#include <qtermwidget5/qtermwidget.h>
|
||||
|
||||
class QTcpSocket;
|
||||
|
||||
class RemoteTerm : public QTermWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
RemoteTerm(const QString &ipaddr, quint16 port, QWidget *parent = 0);
|
||||
public slots:
|
||||
void atError();
|
||||
private:
|
||||
QTcpSocket *socket;
|
||||
};
|
||||
|
||||
#endif // WIDGET_H
|
||||
@@ -0,0 +1,37 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import sys
|
||||
import os
|
||||
import socket
|
||||
import pty
|
||||
|
||||
def usage(program):
|
||||
print "Example(server-side) for remote terminal of QTermWidget."
|
||||
print "Usage: %s ipaddr port" %program
|
||||
|
||||
|
||||
def main():
|
||||
if len(sys.argv) != 3:
|
||||
usage(sys.argv[0])
|
||||
sys.exit(1)
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
try:
|
||||
s.bind((sys.argv[1], int(sys.argv[2])))
|
||||
s.listen(0)
|
||||
print "[+]Start Server."
|
||||
except Exception as e:
|
||||
print "[-]Error Happened: %s" %e.message
|
||||
sys.exit(2)
|
||||
|
||||
while True:
|
||||
c = s.accept()
|
||||
os.dup2(c[0].fileno(), 0)
|
||||
os.dup2(c[0].fileno(), 1)
|
||||
os.dup2(c[0].fileno(), 2)
|
||||
|
||||
# It's important to use pty to spawn the shell.
|
||||
pty.spawn("/bin/sh")
|
||||
c[0].close()
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,87 @@
|
||||
/* Copyright (C) 2008 e_k (e_k@users.sourceforge.net)
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Library General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Library General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Library General Public License
|
||||
along with this library; see the file COPYING.LIB. If not, write to
|
||||
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
|
||||
#include <QApplication>
|
||||
#include <QtDebug>
|
||||
#include <QIcon>
|
||||
#include <QMainWindow>
|
||||
#include <QMenuBar>
|
||||
|
||||
#include "qtermwidget.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
QIcon::setThemeName(QStringLiteral("oxygen"));
|
||||
QMainWindow *mainWindow = new QMainWindow();
|
||||
|
||||
QTermWidget *console = new QTermWidget();
|
||||
|
||||
QMenuBar *menuBar = new QMenuBar(mainWindow);
|
||||
QMenu *actionsMenu = new QMenu(QStringLiteral("Actions"), menuBar);
|
||||
menuBar->addMenu(actionsMenu);
|
||||
actionsMenu->addAction(QStringLiteral("Find..."), console, &QTermWidget::toggleShowSearchBar,
|
||||
QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_F));
|
||||
actionsMenu->addAction(QStringLiteral("Copy"), console, &QTermWidget::copyClipboard,
|
||||
QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_C));
|
||||
actionsMenu->addAction(QStringLiteral("Paste"), console, &QTermWidget::pasteClipboard,
|
||||
QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_V));
|
||||
actionsMenu->addAction(QStringLiteral("About Qt"), &app, &QApplication::aboutQt);
|
||||
mainWindow->setMenuBar(menuBar);
|
||||
|
||||
QFont font = QApplication::font();
|
||||
#ifdef Q_OS_MACOS
|
||||
font.setFamily(QStringLiteral("Monaco"));
|
||||
#elif defined(Q_WS_QWS)
|
||||
font.setFamily(QStringLiteral("fixed"));
|
||||
#else
|
||||
font.setFamily(QStringLiteral("Monospace"));
|
||||
#endif
|
||||
font.setPointSize(12);
|
||||
|
||||
console->setTerminalFont(font);
|
||||
|
||||
// console->setColorScheme(COLOR_SCHEME_BLACK_ON_LIGHT_YELLOW);
|
||||
console->setScrollBarPosition(QTermWidget::ScrollBarRight);
|
||||
|
||||
const auto arguments = QApplication::arguments();
|
||||
for (const QString& arg : arguments)
|
||||
{
|
||||
if (console->availableColorSchemes().contains(arg))
|
||||
console->setColorScheme(arg);
|
||||
if (console->availableKeyBindings().contains(arg))
|
||||
console->setKeyBindings(arg);
|
||||
}
|
||||
|
||||
mainWindow->setCentralWidget(console);
|
||||
mainWindow->resize(600, 400);
|
||||
|
||||
// info output
|
||||
qDebug() << "* INFO *************************";
|
||||
qDebug() << " availableKeyBindings:" << console->availableKeyBindings();
|
||||
qDebug() << " keyBindings:" << console->keyBindings();
|
||||
qDebug() << " availableColorSchemes:" << console->availableColorSchemes();
|
||||
qDebug() << "* INFO END *********************";
|
||||
|
||||
// real startup
|
||||
QObject::connect(console, &QTermWidget::finished, mainWindow, &QMainWindow::close);
|
||||
|
||||
mainWindow->show();
|
||||
return app.exec();
|
||||
}
|
||||
Reference in New Issue
Block a user