Picking input from a LineEdit fails
-
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 thena2
. 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 at0
. If I add a button to the ui and callsendData()
in theonClicked()
function the output is right. So maybe in theServerNet.cpp
file something is wrong with the call ofserverSys.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; }
-
@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?