Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. move CLI text to QLabel
Forum Updated to NodeBB v4.3 + New Features

move CLI text to QLabel

Scheduled Pinned Locked Moved Unsolved General and Desktop
libsshqlabel
2 Posts 2 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rofiquzzaki
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved