//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
) : 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
int buttoncount = ui->listWidget->count();
for(int index = 0; index < buttoncount; 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(){
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
();
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;
}
{
Q_OBJECT
public:
explicit MessageBox
(QWidget *parent
= 0);
~MessageBox();
private:
Ui::MessageBox *ui;
public slots:
void OKClicked();
void RemoveButton();
void AddButton();
void OnRecieve();
};
#endif // MESSAGEBOX_H
//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
To copy to clipboard, switch view to plain text mode
Bookmarks