Alright, so I understand that the program can get away with a millisecond delay while receiving the message so I don't really need a new thread. I've tried to rewrite it accordingly but I keep running into the problem that I need to have the following somewhere in my code:
for(;;)
{
rawmessage = network_getmessage(socket);
emit recvdMsg(rawmessage);
}
for(;;)
{
rawmessage = network_getmessage(socket);
emit recvdMsg(rawmessage);
}
To copy to clipboard, switch view to plain text mode
I've put it in one of the constructors but as I expected it just hangs...
Here's my new, more condensed program (network code omitted):
dialogue.cpp (message display, text input etc)
#include "dialogue.h"
Dialogue::Dialogue(int socket)
{
char *rawmessage = (char *)malloc(MAX_MSG_LENGTH);
commSocket = socket;
layout->addWidget(input);
layout->addWidget(output);
layout->addWidget(submit);
setLayout(layout);
connect(submit, SIGNAL(clicked()), this, SLOT(sendMsg()));
connect(this, SIGNAL(recvdMsg(char *)), this, SLOT(displayMsg(char *)));
for(;;)
{
rawmessage = network_getmessage(socket);
emit recvdMsg(rawmessage);
}
}
void Dialogue::sendMsg(int socket)
{
char *msg = ba.data();
network_sendmessage(socket, msg);
}
void Dialogue::displayMsg(char *msg)
{
output->append(msg);
}
#include "dialogue.h"
Dialogue::Dialogue(int socket)
{
char *rawmessage = (char *)malloc(MAX_MSG_LENGTH);
commSocket = socket;
input = new QLineEdit(this);
output = new QTextEdit(this);
QPushButton *submit = new QPushButton("Submit", this);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(input);
layout->addWidget(output);
layout->addWidget(submit);
setLayout(layout);
connect(submit, SIGNAL(clicked()), this, SLOT(sendMsg()));
connect(this, SIGNAL(recvdMsg(char *)), this, SLOT(displayMsg(char *)));
for(;;)
{
rawmessage = network_getmessage(socket);
emit recvdMsg(rawmessage);
}
}
void Dialogue::sendMsg(int socket)
{
QString qMsg = input->text();
QByteArray ba = qMsg.toLatin1();
char *msg = ba.data();
network_sendmessage(socket, msg);
}
void Dialogue::displayMsg(char *msg)
{
QString qMsg = msg;
output->append(msg);
}
To copy to clipboard, switch view to plain text mode
main.cpp
#include <QApplication>
#include <QWidget>
#include "dialogue.h"
#include "network.h"
{
public:
};
{
int socket = network_tcpconnect();
Dialogue *dialogue = new Dialogue(socket);
layout->addWidget(quit);
layout->addWidget(dialogue);
setLayout(layout);
}
int main(int argc, char *argv[])
{
Client client;
client.show();
return app.exec();
}
#include <QApplication>
#include <QWidget>
#include "dialogue.h"
#include "network.h"
class Client : public QWidget
{
public:
Client(QWidget *parent = 0);
};
Client::Client(QWidget *parent) : QWidget(parent)
{
int socket = network_tcpconnect();
QPushButton *quit = new QPushButton("Quit", this);
Dialogue *dialogue = new Dialogue(socket);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(quit);
layout->addWidget(dialogue);
setLayout(layout);
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Client client;
client.show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks