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. Picking input from a LineEdit fails

Picking input from a LineEdit fails

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtcreator
2 Posts 2 Posters 226 Views
  • 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.
  • C Offline
    C Offline
    Coop4Free
    wrote on last edited by
    #1

    Hi guys,

    I tried to script a network client and server that send datas to each other. My problem in the project is a bug for that I've spent a lot of time to debug it but I couldn't. It is just in the Server. In the ServerSys.cpp file you can see two outputs. The first one should output the input of a LineEdit-Field and then a2. If I execute it the output is:

    0
    a2
    

    In the LineEdit-Field, which I have checked several times that it is the right one, I write things in it like 12345. But the output stays at 0. If I add a button to the ui and call sendData() in the onClicked() function the output is right. So maybe in the ServerNet.cppfile something is wrong with the call of serverSys.sendData() but I don't know. I haven't found any solution.

    Thank you for your answers!

    Main.cpp:

    #include "ServerSys.hpp"
    #include "ServerNet.hpp"
    #include <QApplication>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        ServerNet serverNet;
        ServerSys serverSys;
        serverSys.show();
        return a.exec();
    }
    

    ServerNet.hpp:

    #ifndef TCPSERVER_HPP
    #define TCPSERVER_HPP
    
    #include <QObject>
    #include <QDebug>
    #include <QTcpServer>
    #include <QTcpSocket>
    
    class ServerNet : public QObject
    {
        Q_OBJECT
    
    public:
        explicit ServerNet(QObject *parent = nullptr);
        void sendData(long long);
    
    signals:
    
    public slots:
        void newConnection();
    
    private:
        QTcpServer *server;
        void receiveRequest();
    };
    
    #endif // TCPSERVER_HPP
    

    ServerNet.cpp:

    #include "ServerNet.hpp"
    #include "ServerSys.hpp"
    #include <string>
    #include <iostream>
    #include <QTcpServer>
    #include <QTcpSocket>
    #include <unistd.h>
    
    static QTcpSocket *socket;
    
    ServerNet::ServerNet(QObject *parent) : QObject(parent)
    {
        server = new QTcpServer(this);
        connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));
        server->listen(QHostAddress::Any, 1234);
    }
    
    void ServerNet::newConnection()
    {
        socket = server->nextPendingConnection();
        receiveRequest();
    }
    
    void ServerNet::receiveRequest()
    {
        std::string receivedRequest;
        socket->waitForReadyRead(2000);
        receivedRequest = socket->readAll().toStdString();
        if(receivedRequest == "label270")
        {
            ServerSys serverSys;
            serverSys.sendData();
        }
        else
        {
            std::cout << "No valid data request" << std::endl;
            exit(1);
        }
    }
    
    void ServerNet::sendData(long long decLabel)
    {
        socket->write(std::to_string(decLabel).c_str());
    }
    

    ServerSys.hpp:

    #ifndef SERVERSYS_HPP
    #define SERVERSYS_HPP
    
    #include <QMainWindow>
    #include <vector>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class ServerSys; }
    QT_END_NAMESPACE
    
    class ServerSys : public QMainWindow
    {
        Q_OBJECT
    
    public:
        ServerSys(QWidget *parent = nullptr);
        ~ServerSys();
        void sendData();
    
    private slots:
    
    private:
        Ui::ServerSys *ui;
        void getData(std::vector<long long>&);
    };
    #endif // SERVERSYS_HPP
    

    ServerSys.cpp:

    #include "ServerSys.hpp"
    #include "ui_ServerSys.h"
    #include "ServerNet.hpp"
    #include <iostream>
    #include <iomanip>
    #include <sstream>
    #include <math.h>
    #include <algorithm>
    #include <vector>
    #include <string>
    #include <QDesktopWidget>
    
    ServerSys::ServerSys(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::ServerSys)
    {
        ui->setupUi(this);
    }
    
    ServerSys::~ServerSys()
    {
        delete ui;
    }
    
    void ServerSys::sendData()
    {
        std::vector<long long> decTokens(8);
        std::vector<std::string> binTokens(8);
        std::vector<bool> binLabel;
        long long decLabel;
    
        getData(decTokens);
    
        ServerNet serverNet;
        serverNet.sendData(decLabel);
    }
    
    void ServerSys::getData(std::vector<long long> &decTokens)
    {
        std::cout << ui->lineEdit_label_270->text().toLongLong() << std::endl;
        decTokens[0] = ui->lineEdit_label_270->text().toLongLong();
        decTokens[1] = ui->lineEdit_sdi_270->text().toLongLong();
        decTokens[2] = ui->lineEdit_distance_270->text().toLongLong();
        decTokens[3] = ui->lineEdit_lsb_270->text().toLongLong();
        decTokens[4] = ui->lineEdit_msb_270->text().toLongLong();
        decTokens[5] = 0;
        decTokens[6] = ui->lineEdit_ssm_270->text().toLongLong();
        decTokens[7] = ui->lineEdit_parity_270->text().toLongLong();
        std::cout << "a2" << std::endl;
    }
    
    JonBJ 1 Reply Last reply
    0
    • C Coop4Free

      Hi guys,

      I tried to script a network client and server that send datas to each other. My problem in the project is a bug for that I've spent a lot of time to debug it but I couldn't. It is just in the Server. In the ServerSys.cpp file you can see two outputs. The first one should output the input of a LineEdit-Field and then a2. If I execute it the output is:

      0
      a2
      

      In the LineEdit-Field, which I have checked several times that it is the right one, I write things in it like 12345. But the output stays at 0. If I add a button to the ui and call sendData() in the onClicked() function the output is right. So maybe in the ServerNet.cppfile something is wrong with the call of serverSys.sendData() but I don't know. I haven't found any solution.

      Thank you for your answers!

      Main.cpp:

      #include "ServerSys.hpp"
      #include "ServerNet.hpp"
      #include <QApplication>
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          ServerNet serverNet;
          ServerSys serverSys;
          serverSys.show();
          return a.exec();
      }
      

      ServerNet.hpp:

      #ifndef TCPSERVER_HPP
      #define TCPSERVER_HPP
      
      #include <QObject>
      #include <QDebug>
      #include <QTcpServer>
      #include <QTcpSocket>
      
      class ServerNet : public QObject
      {
          Q_OBJECT
      
      public:
          explicit ServerNet(QObject *parent = nullptr);
          void sendData(long long);
      
      signals:
      
      public slots:
          void newConnection();
      
      private:
          QTcpServer *server;
          void receiveRequest();
      };
      
      #endif // TCPSERVER_HPP
      

      ServerNet.cpp:

      #include "ServerNet.hpp"
      #include "ServerSys.hpp"
      #include <string>
      #include <iostream>
      #include <QTcpServer>
      #include <QTcpSocket>
      #include <unistd.h>
      
      static QTcpSocket *socket;
      
      ServerNet::ServerNet(QObject *parent) : QObject(parent)
      {
          server = new QTcpServer(this);
          connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));
          server->listen(QHostAddress::Any, 1234);
      }
      
      void ServerNet::newConnection()
      {
          socket = server->nextPendingConnection();
          receiveRequest();
      }
      
      void ServerNet::receiveRequest()
      {
          std::string receivedRequest;
          socket->waitForReadyRead(2000);
          receivedRequest = socket->readAll().toStdString();
          if(receivedRequest == "label270")
          {
              ServerSys serverSys;
              serverSys.sendData();
          }
          else
          {
              std::cout << "No valid data request" << std::endl;
              exit(1);
          }
      }
      
      void ServerNet::sendData(long long decLabel)
      {
          socket->write(std::to_string(decLabel).c_str());
      }
      

      ServerSys.hpp:

      #ifndef SERVERSYS_HPP
      #define SERVERSYS_HPP
      
      #include <QMainWindow>
      #include <vector>
      
      QT_BEGIN_NAMESPACE
      namespace Ui { class ServerSys; }
      QT_END_NAMESPACE
      
      class ServerSys : public QMainWindow
      {
          Q_OBJECT
      
      public:
          ServerSys(QWidget *parent = nullptr);
          ~ServerSys();
          void sendData();
      
      private slots:
      
      private:
          Ui::ServerSys *ui;
          void getData(std::vector<long long>&);
      };
      #endif // SERVERSYS_HPP
      

      ServerSys.cpp:

      #include "ServerSys.hpp"
      #include "ui_ServerSys.h"
      #include "ServerNet.hpp"
      #include <iostream>
      #include <iomanip>
      #include <sstream>
      #include <math.h>
      #include <algorithm>
      #include <vector>
      #include <string>
      #include <QDesktopWidget>
      
      ServerSys::ServerSys(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::ServerSys)
      {
          ui->setupUi(this);
      }
      
      ServerSys::~ServerSys()
      {
          delete ui;
      }
      
      void ServerSys::sendData()
      {
          std::vector<long long> decTokens(8);
          std::vector<std::string> binTokens(8);
          std::vector<bool> binLabel;
          long long decLabel;
      
          getData(decTokens);
      
          ServerNet serverNet;
          serverNet.sendData(decLabel);
      }
      
      void ServerSys::getData(std::vector<long long> &decTokens)
      {
          std::cout << ui->lineEdit_label_270->text().toLongLong() << std::endl;
          decTokens[0] = ui->lineEdit_label_270->text().toLongLong();
          decTokens[1] = ui->lineEdit_sdi_270->text().toLongLong();
          decTokens[2] = ui->lineEdit_distance_270->text().toLongLong();
          decTokens[3] = ui->lineEdit_lsb_270->text().toLongLong();
          decTokens[4] = ui->lineEdit_msb_270->text().toLongLong();
          decTokens[5] = 0;
          decTokens[6] = ui->lineEdit_ssm_270->text().toLongLong();
          decTokens[7] = ui->lineEdit_parity_270->text().toLongLong();
          std::cout << "a2" << std::endl;
      }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Coop4Free said in Picking input from a LineEdit fails:

      long long decLabel;
      
      getData(decTokens);
      
      ServerNet serverNet;
      serverNet.sendData(decLabel);
      

      decLabel is an uninitialized variable. What else is there to say?

      1 Reply Last reply
      1

      • Login

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