[英]Qt/C++ CLI text to QLabel
我正在尝试使用Qt GUI通过libssh发送远程命令。 在此之前,我尝试使用Qt控制台应用程序通过libssh发送远程命令。 如何从CLI读取数据并将其移至QLabel? 这来自我的CLI程序main.cpp
...
char buffer[256];
int nbytes;
nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
while (nbytes > 0)
{
if (fwrite(buffer, 1, nbytes, stdout) != nbytes)
{
ssh_channel_close(channel);
ssh_channel_free(channel);
return SSH_ERROR;
}
nbytes = ssh_channel_read(channel, buffer, sizeof(buffer), 0);
}
if (nbytes < 0)
{
ssh_channel_close(channel);
ssh_channel_free(channel);
return SSH_ERROR;
}
我正在尝试将其移至QLabel setatus.cpp
#include <QString>
#include <libssh/libssh.h>
#include "setatus.h"
#include <stdio.h>
#include <stdlib.h>
QString masukan;
char bufr[256];
int nbytes;
int statserv(ssh_session session)
{
ssh_channel channel;
int rc;
channel = ssh_channel_new(session);
rc = ssh_channel_open_session(channel);
if (rc != SSH_OK)
{
ssh_channel_free(channel);
return rc;
}
masukan = "uname -a";
QByteArray masuk = masukan.toUtf8();
char* msk = masuk.data();
rc = ssh_channel_request_exec(channel, msk);
if (rc != SSH_OK)
{
ssh_channel_close(channel);
ssh_channel_free(channel);
return rc;
}
//char bufr[256];
//int nbytes;
nbytes = ssh_channel_read(channel, bufr, sizeof(bufr), 0);
while (nbytes > 0)
{
if (fwrite(bufr, 1, nbytes, stdout) != nbytes)
{
ssh_channel_close(channel);
ssh_channel_free(channel);
return SSH_ERROR;
}
nbytes = ssh_channel_read(channel, bufr, sizeof(bufr), 0);
}
if (nbytes < 0)
{
ssh_channel_close(channel);
ssh_channel_free(channel);
return SSH_ERROR;
}
ssh_channel_send_eof(channel);
ssh_channel_close(channel);
ssh_channel_free(channel);
return SSH_OK;
//return ui->label->setText(nbytes);
//connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
}
setatus.h
#ifndef SETATUS_H
#define SETATUS_H
#include <libssh/libssh.h>
extern int nbytes;
int statserv(ssh_session session);
class setatus
{
public:
setatus();
};
#endif // SETATUS_H
我试图在main.cpp中调用它
...
statserv(sesi_ssh);
//setatus.statserv(sesi_ssh);
gambar.show();
...
这是我的ui rese.cpp
#include "rese.h"
#include "ui_rese.h"
#include "setatus.h"
#include <libssh/libssh.h>
//ssh_session sesi_ssh;
//int nbytes;
rese::rese(QWidget *parent) :
QWidget(parent),
ui(new Ui::rese)
{
ui->setupUi(this);
//ui->tulisan->setText("status");
//statserv(sesi_ssh);
//label = new QLabel(this);
//ui->tulisan->setText(nbytes);
QString tls = QString::number(nbytes);
ui->tulisan->setText(tls);
}
rese::~rese()
{
delete ui;
}
但QLabel仅显示“ 0”。 有人能帮助我吗。 我是Qt和C ++的新手。 我正在尝试制作使用Qt GUI应用程序通过libssh管理服务器的软件。 提前致谢。
您可以使用QProcess
与子流程进行交互。 要读取其输出,可以使用QProcess::readAllStandardOutput
取自qtcentre的示例 :
QString program = "cmd.exe";
QStringList arguments;
arguments << "-v";
QProcess *myProcess = new QProcess();
myProcess->start(program);
if (myProcess->waitForStarted(1000) == false)
qDebug() << "Error starting external program";
else
qDebug() << "external program running";
myProcess->waitForReadyRead(100);
myProcess->waitForFinished(100);
qDebug() << "read output" << myProcess->readAllStandardOutput();
myProcess->write("dir \n");
myProcess->closeWriteChannel();
myProcess->waitForBytesWritten(100);
myProcess->waitForReadyRead(100);
myProcess->waitForFinished(100);
qDebug() << "read output" << myProcess->readAllStandardOutput()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.