PDA

View Full Version : c++ TcpSocket readAll from other form help



jwz104
6th April 2016, 16:42
I am trying to send and receive data to/from a client with a qtcpserver. This is working, but now I want to send and receive it with an other form. sending the data works. But receiving not. The readReady signal is emitted, but when I do readAll/readLine the string is empty.

This is the source:


//MainWindow
//This is how I open the dialog from the main form, clientSocket is the tcpsocket where I want to send and receive data from.
MessageBox msgform;
msgform.socket = clientSocket;
msgform.setModal(true);
msgform.exec();










//messagebox
//This is the full source of messagebox.cpp
#include "messagebox.h"
#include "mainwindow.h"
#include "ui_messagebox.h"

MessageBox::MessageBox(QWidget *parent) :
QDialog(parent),
ui(new Ui::MessageBox)
{
ui->setupUi(this);
//connect(ui->buttonBox, &QDialogButtonBox::accepted, this, &MessageBox::OKClicked);
connect(ui->pushButton_3, &QPushButton::clicked, this, &MessageBox::OKClicked);
connect(ui->pushButton, &QPushButton::clicked, this, &MessageBox::RemoveButton);
connect(ui->pushButton_2, &QPushButton::clicked, this, &MessageBox::AddButton);
}

MessageBox::~MessageBox()
{
delete ui;
}

void MessageBox::OKClicked(){
QTcpSocket *socket2 = socket;//if I try to use socket it gives a lot of errors so I have to use socket2
connect(socket2, SIGNAL(readyRead()), this, SLOT(OnRecieve())); //connecting readready when data has been send
QString buttonArray;
int buttoncount = ui->listWidget->count();
for(int index = 0; index < buttoncount; index++){
QListWidgetItem * item = ui->listWidget->item(index);
if(index == 0){
buttonArray = buttonArray + item->text();
}else{
buttonArray = buttonArray + "-+-" + item->text();
}
}
socket2->write("MESSAGE|" + ui->lineEdit->text().toLocal8Bit() + "|" + ui->plainTextEdit->toPlainText().toLocal8Bit() + "|" + QString::number(ui->comboBox->currentIndex()).toLocal8Bit() + "|" + buttonArray.toLocal8Bit()); //This is send succesfully
}

void MessageBox::RemoveButton(){
qDeleteAll(ui->listWidget->selectedItems());
}

void MessageBox::AddButton(){
QListWidgetItem *buttonName = new QListWidgetItem;
buttonName->setText("Button name");
buttonName->setFlags (buttonName->flags () | Qt::ItemIsEditable);
ui->listWidget->addItem(buttonName);
}

void MessageBox::OnRecieve(){//is executed when I receive data
QTcpSocket *socket2 = static_cast<QTcpSocket*>(sender());
QString receivedText = socket2->readAll();
QStringList receivedArray = receivedText.split("|");
qDebug() << receivedText;//this is always empty
}











//full source of messagebox.h
#ifndef MESSAGEBOX_H
#define MESSAGEBOX_H

#include <QDialog>
#include <QDebug>
#include <QTcpServer>
#include <QTcpSocket>

namespace Ui {
class MessageBox;
}

class MessageBox : public QDialog
{
Q_OBJECT

public:
explicit MessageBox(QWidget *parent = 0);
~MessageBox();
QTcpSocket *socket;
private:
Ui::MessageBox *ui;
public slots:
void OKClicked();
void RemoveButton();
void AddButton();
void OnRecieve();
};

#endif // MESSAGEBOX_H

Could someone help me with this?
And if you see any other mistake please say it, this is my third day of qt c++

anda_skoa
6th April 2016, 17:33
So something else connected to the readyRead() signal and maybe also reads from the socket?

Cheers,
_

jwz104
6th April 2016, 20:41
In the messagebox.cpp there is only one connection to the readyRead()
But I have a readyRead connection in mainwindow.cpp that is listening for all clients but that doesn't matter right?(Please correct me if I am wrong)

anda_skoa
7th April 2016, 09:23
In the messagebox.cpp there is only one connection to the readyRead()
But I have a readyRead connection in mainwindow.cpp that is listening for all clients but that doesn't matter right?(Please correct me if I am wrong)

Well, if the slot in the main window doesn't do anything, then it doesn't matter.
But if it also reads, then that data is no longer available to any other slot.

Cheers,
_

jwz104
7th April 2016, 09:42
Ah than that is my problem, that slot also reads data, but do you know how I can still read the data on messagebox.cpp?
I think a good solution is to connect the readready SLOT from mainwindow to the readready SLOT from messagebox. But is this possible?

anda_skoa
7th April 2016, 10:09
Maybe you need to rethink what you are doing.

Why did you end up with two places that compete for socket data?

Cheers,
_

jwz104
7th April 2016, 16:42
I now know how I can get this working but it isnt fully working. When the readready on the mainform reads something it emits an signal with the peerport and the text it received. Now I want to connect this signal to my new form. This is how I try to do it:

MessageBox msgform;
msgform.socket = clientSocket;
connect(this, SIGNAL(RecieveSignal(int,QString)), msgform, SLOT(OnRecieve(int,QString))); //line 150
msgform.setModal(true);
msgform.exec();

Only when I try to run it I get this error on line 150:

error: no matching function for call to 'MainWindow::connect(MainWindow* const, const char*, MessageBox&, const char*)'
connect(this, SIGNAL(RecieveSignal(int,QString)), msgform, SLOT(OnRecieve(int,QString)));
^
The signal and the slot both contain an int and a QString. How can I fix this?

Lesiok
7th April 2016, 18:16
Line 3 (150) :
connect(this, SIGNAL(RecieveSignal(int,QString)), &msgform, SLOT(OnRecieve(int,QString))); //line 150

jwz104
7th April 2016, 21:30
Thanks! It no fully works!