move CLI text to QLabel



  • I'm trying to send a remote command over libssh using Qt GUI.
    Before this, I've tried to send remote command over libssh using Qt console Application. How can I read data from CLI and move it to QLabel?
    this' from my CLI Program
    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;
    }
    

    and i'm trying to move it to 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
    
    

    I'm trying to call it in
    main.cpp

    ...
                    statserv(sesi_ssh);
                    //setatus.statserv(sesi_ssh);
                    gambar.show();
    ...
    

    and this' my 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;
    }
    
    
    
    

    but the QLabel just show "0".
    can someone help me, please. I'm new to Qt and new to C++.
    I'm trying to make software to manage server over libssh using Qt GUI Application.
    Thanks in advance.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Rather than using extern variables you should encapsulate your ssh related code in a class and use it from your GUI. It will be easier to interface with it.

    Hope it helps


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.