Qt / C ++ CLI文本到QLabel

时间:2015-12-03 07:45:36

标签: c++ qt

我尝试使用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();
...

这个&#39;我的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只显示&#34; 0&#34;。 有谁可以帮助我吗。我是Qt的新手,也是C ++的新手。 我正在尝试使用Qt GUI Application制作软件以管理libssh上的服务器。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

您可以使用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()