Use of QAbstractSocket stateChanged()-Signal - c++

What is the correct use/implementation of the QAbstractSocket stateChanged()-Signal?
main.cpp:
#include <QCoreApplication>
#include <sslserver.h>
#include <QLoggingCategory>
int main(int argc, char *argv[]){
QCoreApplication a(argc, argv);
QLoggingCategory::setFilterRules("*.debug=true");
SslServer *myTestServer = new SslServer();
return a.exec();
}
SslServer.h:
#ifndef SSLSERVER_H
#define SSLSERVER_H
#include <QObject>
#include <QTcpServer>
#include <QTcpSocket>
#include <QTimer>
class SslServer : public QObject
{
Q_OBJECT
public:
explicit SslServer(QObject *parent = nullptr);
private slots:
void newTestConnection();
void sendTestdata();
void connectionWasClosed(QAbstractSocket::SocketState state = QAbstractSocket::UnconnectedState);
private:
QTcpServer *testServer;
QTcpSocket *testSocket;
QTimer *testTimer;
};
#endif // SSLSERVER_H
SslServer.cpp:
#include "sslserver.h"
#include <QDebug>
SslServer::SslServer(QObject *parent) : QObject(parent){
testServer = new QTcpServer(this);
testSocket = new QTcpSocket();
testTimer = new QTimer();
connect(testServer, SIGNAL(newConnection()), this, SLOT(newTestConnection())); //works
connect(testTimer, SIGNAL(timeout()), this, SLOT(sendTestdata())); //works
connect(testSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(connectionWasClosed(QAbstractSocket::SocketState)));
//Qt5 Connection was also tested, without any change to old connection-typo
//connect(testSocket, &QAbstractSocket::stateChanged, this, &SslServer::verbindungWurdeBeendet);
testTimer->start(1000);
if(testServer->listen(QHostAddress::Any, 9999)){
qDebug() << "Server running on Port 9999";
}else{
qDebug() << "Error while building server";
}
}
void SslServer::newTestConnection(){
testSocket->close();
testSocket = testServer->nextPendingConnection();
testSocket->write("Welcome on: Testserver!");
testSocket->flush();
}
void SslServer::sendTestdata(){
if(testSocket->isOpen()){
qDebug() << testSocket->state();
qDebug() << "Sending testdata to client";
testSocket->write("testdata");
}
}
void SslServer::connectionWasClosed(QAbstractSocket::SocketState){
qDebug() << "!!! SocketState changed !!!";
}
I can see from the testSocket->state() statement that the state is changing from Unconnected to Connected to Unconnected, but the SLOT-Function is never called. What am I missing here?

The problem is caused because you are connecting to an object:
testSocket = new QTcpSocket();
But you expect to receive the signal from another object:
testSocket = testServer->nextPendingConnection();
The solution is to make the connection when you receive the new object:
void SslServer::newTestConnection(){
if(testSocket)
testSocket->close();
testSocket = testServer->nextPendingConnection();
connect(testSocket, &QAbstractSocket::stateChanged, this, &SslServer::connectionWasClosed);
testSocket->write("Welcome on: Testserver!");
testSocket->flush();
}
The complete example can be found in the following link

Your SLOT signature is not matching with SIGNAL.
Change the function declaration and definition to
void verbindungWurdeBeendet(QAbstractSocket::SocketState state);
And the connect statement to
connect(testSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), this, SLOT(verbindungWurdeBeendet(QAbstractSocket::SocketState)));
The SLOT function verbindungWurdeBeendet() (with out argument) is also acceptable, if you have default value in the function declaration.
ex:
In the below case you can have a SLOT in connect statement, without arguments.
void verbindungWurdeBeendet(QAbstractSocket::SocketState state = QAbstractSocket::UnconnectedState);

Related

Using Thread in GUI application

I am making a multiple game. I need to take command from client. This means I have GUI and TcpServer. So I need to work them simultaneously. I used Thread but it doesnt work. Could you please help me to find the problem?
Summarizing: Firstly player press the "online" button. Then Oyun() Gui function runs and button connected with connectPressed() function. In this function there is a thread in order to run read the client commands when Gui is working.
Firstly I used QTimer in order to take command from Client in every 1 second. But My GUI freezed. And Then I used QThread but according to my research, QThread is not proper for GUI app. So I found Qtconcurrent, QFutureWatcher and QFuture. But my thread is still not working. I should have made a mistake somewhere.
#include <QApplication>
#include <anaclass.h>
#include <player2.h>
#include <tcpserver.h>
#include <QThread>
#include <QObject>
//#include <worker.h>
AnaClass *anaclass;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
anaclass = new AnaClass();
anaclass->show();
anaclass->Giris(); //button selection page
return a.exec();
}
#include "anaclass.h"
Puan *puanlama1;
Puan *puanlama2;
player2 *yilan2;
AnaClass::AnaClass() : QGraphicsView()
{
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
setFixedSize(800,600);
scene = new QGraphicsScene;
scene->setSceneRect(0,0,800,600);
setScene(scene);
}
void AnaClass::Giris()
{
connectButton = new Button("Online");
double cxPos = this->width()/2 - connectButton->boundingRect().width()/2;
double cyPos= 425;
connectButton->setPos(cxPos, cyPos);
connect(connectButton, SIGNAL(clicked()), this, SLOT(connectPressed()));
scene->addItem(connectButton);
}
void AnaClass::Oyun()
{
scene->clear();
puanlama1 = new Puan();
puanlama1->setDefaultTextColor(Qt::blue);
puanlama1->setPos(5, 2);
scene->addItem(puanlama1);
yilan = new Yilan();
yilan->setRect(0,0,19,19);
scene->addItem(yilan);
yilan->setFlags(QGraphicsItem::ItemIsFocusable);
yilan->setFocus();
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(Qt::blue);
yilan->setBrush(brush);
if(stringButtonName == "Player2" || stringButtonName == "Online")
{
yilan->setPos(scene->width()/2 + 60, scene->height()/2);
}
else
{
yilan->setPos(scene->width()/2, scene->height()/2);
}
if(stringButtonName == "Player2" || stringButtonName == "Online")
{
yilan->playerNumber=1;
puanlama2 = new Puan();
puanlama2->setDefaultTextColor(Qt::green);
puanlama2->setPos(700, 2);
scene->addItem(puanlama2);
yilan2 = new player2();
yilan2->setRect(0,0,19,19);
scene->addItem(yilan2);
yilan2->setFlags(QGraphicsItem::ItemIsFocusable);
yilan2->setFocus();
QBrush brush2;
brush2.setStyle(Qt::SolidPattern);
brush2.setColor(Qt::green);
yilan2->setBrush(brush2);
yilan2->setPos(scene->width()/2 - 60,scene->height()/2);
}
emit emitTcp();
}
void AnaClass::connectPressed()
{
qDebug()<<"connect basildi";
server = new TCPServer();
server->Test();
stringButtonName = connectButton->buttonName;
qDebug()<<"Gelen Veri " + server->OkunanBilgi;
QFutureWatcher<void> watcher;
connect(&watcher, SIGNAL(finished()), server, SLOT(daimaOku()), Qt::QueuedConnection);
QFuture<void> deneme = QtConcurrent::run(this, &AnaClass::emitTcp);
watcher.setFuture(deneme);
Oyun();
}
}
#ifndef ANACLASS_H
#define ANACLASS_H
#include <QGraphicsScene>
#include <QGraphicsView>
#include <yilan.h>
#include <Meyve.h>
#include <QBrush>
#include <Puan.h>
#include <player2.h>
#include <QThread>
#include <label.h>
#include <QKeyEvent>
#include <button.h>
#include <QDebug>
#include <tcpserver.h>
#include <QTime>
#include <QTimer>
#include <QMutex>
//#include <worker.h>
#include <QFuture>
#include <QtConcurrent>
#include <QFutureWatcher>
class AnaClass : public QGraphicsView
{
Q_OBJECT
public:
AnaClass();
void Giris();
void Oyun();
void timerEvent(QTimerEvent *event);
void keyPressEvent(QKeyEvent *event2);
public:
Yilan *yilan;
//QThread *thread;
QGraphicsScene *scene;
Label *label1;
Button* player1Button;
Button* player2Button;
Button* connectButton;
TCPServer *server;
QTimer *timerOnline;
public:
int k=0;
int t=0;
QString stringButtonName;
signals:
void emitTcp();
public slots:
void connectPressed();
void player1Pressed();
void player2Pressed();
};
#endif // ANACLASS_H
#define TCPSERVER_H
#include <QTcpServer>
#include <QTcpSocket>
#include <QDebug>
#include <QObject>
#include <QTimer>
class TCPServer : public QObject
{
Q_OBJECT
public:
TCPServer(QObject* parent = nullptr);
void Test();
signals:
//void emitTcp();
public slots:
void newConnection();
void daimaOku(); // always read as english
public:
QTcpServer *server;
QTcpSocket *socket;
QTimer *timerTcp;
QString OkunanBilgi;
};
#endif // TCPSERVER_H
#include "tcpserver.h"
TCPServer::TCPServer(QObject * parent) : QObject()
{
}
void TCPServer::Test()
{
server = new QTcpServer();
connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));
if(!server->listen(QHostAddress::Any, 1234))
{
qDebug()<<"server baslamadi";
}
else
{
qDebug()<<"server basladi";
}
//timerTcp = new QTimer();
//connect(timerTcp, SIGNAL(timeout()), this, SLOT(daimaOku()));
//emit emitTcp();
}
void TCPServer::newConnection()
{
qDebug()<<"newconnection";
socket = server->nextPendingConnection();
socket->write("Merhaba Client");
socket->flush();
socket->waitForBytesWritten(5000);
timerTcp->start(50);
}
void TCPServer::daimaOku() //alwaysread() as english
{
qDebug()<<"always read function is working";
// if(socket->state() == QAbstractSocket::ConnectedState)
// {
// qDebug()<<"Daima oku fonsiyonu soket bagli";
// socket->waitForReadyRead();
// OkunanBilgi = socket->readAll();
// qDebug()<<"Tcp daima oku :" + OkunanBilgi;
// }
}
Thank you for your comments. I solved the problem by deleting QFuture and adding connect() like below.
timerOnline = new QTimer();
connect(timerOnline, SIGNAL(timeout()), server, SLOT(daimaOku()));
timerOnline->start(500);
But I have another problem. When Client connects the server, my Gui app freezes. You can find the revised code below.
void TCPServer::daimaOku()
{
qDebug()<<"Function is running";
if(socket->state() == QAbstractSocket::UnconnectedState)
{
qDebug()<<"Socket is not connected";
}
else
{
qDebug()<<"Socket connected";
socket->waitForReadyRead();
OkunanBilgi = socket->readAll();
qDebug()<<"Tcp always read :" + OkunanBilgi;
}
}
When client is not connected, the output is:
Function is running
Socket is not connected
Socket is not connected ...
I can play the game but when client is connected, game freezes. I don't understand why.

pass data from one class to another qt

I want to pass data from one form to another. I did some research and I found out how to do it, but my code doesn't work!!
Here is my code:
MainWindow.h
signals:
void signalEmission(const String &port) ;
MainWindow.cpp
void MainWindow::on_pushButtonclicked()
{
emit signalEmission(nameArea->toPlainText()) ;
}
class.h
private slots:
void mySlot(const QString &port) ;
class.cpp
void class::mySlot(const QString &port)
{
messages->append(port) ;
}
main.cpp
MainWindow h ;
class c;
QObject::connect(&h,SIGNAL(signalEmission(const QString&)),&c,SLOT(research(const QString&))) ;
I don't get the port number from the MainWindow class!!
Can someone please help.
EDIT:
Here is my code
history.h
#ifndef HISTORY_H
#define HISTORY_H
#include <QMainWindow>
#include <ui_history.h>
#include <QTcpSocket>
class History : public QMainWindow, private Ui::History
{
Q_OBJECT
public:
explicit History();
private slots:
void on_searchButton_clicked();
signals:
void signalEmission( QString port) ;
};
#endif // HISTORY_H
history.cpp
#include "history.h"
#include "ui_history.h"
#include "client.h"
#include "ui_client.h"
#include <QDebug>
#include <QtNetwork>
History::History() {
setupUi(this);
resize(500,200);
move(300,300) ;
}
void History::on_searchButton_clicked()
{
emit this->signalEmission(receiverPort->toPlainText()) ;
this->close() ;
}
client.h
#ifndef HEADER_CLIENT
#define HEADER_CLIENT
#include <QMainWindow>
#include <QtWidgets>
#include <QtNetwork>
#include <QSqlDatabase>
#include "history.h"
#include "ui_client.h"
class client : public QMainWindow, private Ui::client
{
Q_OBJECT
public:
explicit client();
public slots:
void on_connexionButton_clicked();
void on_sendButton_clicked();
void receivedData();
void connectClient();
void disconnect();
void socketError(QAbstractSocket::SocketError erreur);
void on_disconnexionButton_clicked();
void on_connectedClientsButton_clicked();
void on_refreshButton_clicked();
void on_historyButton_clicked();
void research( QString port) ;
private slots:
void on_clearButton_clicked();
void on_connectedList_pressed();
private:
QTcpSocket *socket; // Représente le serveur
quint16 messageLength;
History *his;
QSqlDatabase db ;
QStringList *cc ;
QStringListModel *ccl ;
QString selectedTexts ;
};
#endif
client.cpp
#include "client.h"
#include "history.h"
#include "ui_history.h"
#include "ui_client.h"
#include <QMainWindow>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QDebug>
client::client()
{
setupUi(this);
cc = new QStringList();
ccl = new QStringListModel() ;
socket = new QTcpSocket(this);
connect(socket, SIGNAL(readyRead()), this, SLOT(receivedData()));
connect(socket, SIGNAL(connected()), this, SLOT(connectClient()));
connect(socket, SIGNAL(disconnected()), this, SLOT(disconnect()));
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(socketError(QAbstractSocket::SocketError)));
messageLength = 0;
disconnexionButton->setEnabled(false);
connectedClientsButton->setEnabled(false);
sendButton->setEnabled(false);
clearButton->setEnabled(false);
refreshButton->setEnabled(false);
historyButton->setEnabled(false);
}
void client::research(QString port){
qDebug() << port ;
//messagesList->cleanHistory() ;
/* QSqlQuery qry(db);
qry.prepare("SELECT * FROM conversations where sender=? and receiver=?");
qry.addBindValue(socket->localPort()) ;
qry.addBindValue(port) ;
if(!qry.exec()){
qDebug() << "Problem with Select conversation" ;
}
else{
qDebug() << "Select conversation query executed successfully" ;
}
messagesList->append(tr("<strong>Historique des conversations:</strong>")) ;
while(qry.next())
{
messagesList->append(qry.value(2).toString()+" "+qry.value(3).toString()) ;
}*/
}
Can someone please tell me what's wrong. It's driving me crazy!!
There are so many little issues here, looking at the comments, this code does not appear to be the real code anyway...? it is better to post the real code then to send some very buggy example - it will confuse people trying to help you.
Anyway, a few things to try to help you:
Is your class "class" really called "class"?, this is a key word
already, so I would rename that, I have no idea what the compiler will make of that :o
Make sure all the function signatures are identical for your slots and signals, i.e. they all take "const QString &".
Check the connection return value is true (ok) and not false (failed):
if (!QObject::connect(...)) {printf("Connection failed!\n");}. If its false then something is wrong.
Try a simple start-point, i.e. make a slot and signal that take a QString such that:
void MainWindow::on_pushButtonclicked()
{
emit signalEmission((QString) "test");
}
finally, please show any errors/warnings from compilation and from debug output.

Thread sending http request using Qt

I am trying to create a thread (HttpWorker) that when required wakes up and sends a http request. I would like this to be done in a single thread. I am using Qt for the implementation.
The way I thought i would do it is to have a class MyHttpWorker, move it to another thread, connect the slots/signals etc. Then on thread start I would use QNetworkAccessManager to call get requests. I would use QWaitCondition to pause the thread after the request has been sent and I would resume this thread whenever I need to send another one.
However, when I pause the httpworker thread, the FinishedSlot is not called at all. If I use the class to simply call one http request, it executes with no problem. So the problem is connected to QWaitCondition (or just freezing the threads in general).
I could simply create and destroy one worker and thread for each request I have, but I require to send lot of http requests, so I think this method would be way too consuming (creating threads and destroying them over and over).
I appreciate any help I can get.
Here is my code:
MyHttpWorker.h
#include <QNetworkReply>
#include <QDebug>
#include <QObject>
#include <QNetworkAccessManager>
#include <QThread>
#include <QWaitCondition>
#include <QMutex>
class MyHttpWorker : public QObject
{
Q_OBJECT
QNetworkAccessManager* nam;
QMutex syncPause;
QWaitCondition pauseCond;
public:
explicit MyHttpWorker(QObject *parent = 0);
void MyWake();
public slots:
void SetTheThread(QThread* thread);
void MyStart();
void finishedSlot(QNetworkReply* reply);
};
MyHttpWorker.cpp
MyHttpWorker::MyHttpWorker(QObject *parent) :
QObject(parent)
{
nam = new QNetworkAccessManager(this);
QObject::connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(finishedSlot(QNetworkReply*)));
}
void MyHttpWorker::finishedSlot(QNetworkReply* reply)
{
qDebug() << "Finished"; //This slot is never even reached, when i used QWaitCond...
if (reply->error() == QNetworkReply::NoError)
{
QByteArray bytes = reply->readAll();
QString string(bytes);
qDebug() << string;
}else
{
qDebug() << reply->errorString();
}
reply->deleteLater();
}
void MyHttpWorker::SetTheThread(QThread* thread){
QObject::connect(thread,SIGNAL(started()),this,SLOT(MyStart()));
}
void MyHttpWorker::MyWake(){
pauseCond.wakeAll();
}
void MyHttpWorker::MyStart(){
qDebug() << "Start" ;
while(true){
syncPause.lock();
qDebug() << "thread waiting...";
pauseCond.wait(&syncPause);
qDebug() << "thread resumed.";
syncPause.unlock();
//sending the actual request here
QNetworkRequest myRequest;
myRequest.setUrl(QUrl("http://www.google.com"));
nam->get(myRequest);
}
}
main.cpp
#include <QCoreApplication>
#include <QThread>
#include <QDebug>
#include <myhttpworker.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//create the worker, thread and launch it... (worker is waiting by default)
MyHttpWorker* worker = new MyHttpWorker;
QThread* httpThread = new QThread;
worker->SetTheThread(httpThread);
worker->moveToThread(httpThread);
httpThread->start();
//try and send 5 requests ...
for(int i=0;i<5;i++){
qDebug() << "Unpausing";
QThread::currentThread()->msleep(1000);
worker->MyWake();
}
return a.exec();
}
don't create an infinite loop but let the even loop handle it:
void MyHttpWorker::MyWake()
{
QMetaObject::invokeMethod(this,"doSend");
//send to the event loop
}
// new slot
void MyHttpWorker::doSend(){
//sending the actual request here
QNetworkRequest myRequest;
myRequest.setUrl(QUrl("http://www.google.com"));
nam->get(myRequest);
}
//and remove the myStart and all that synchronisation
then when you want to stop it just send a quit to the thread. I suggest you also connect the finished signal of the thread to the deleteLater slot of MyHttpWorker

QObject: no such slot QThread::readyRead()

I am trying to figure out how to correctly use Qt TCP Sockets, as well as multithreading. I want, as a test project in support of something more complex but similar I will try in the future, to do the following: a simple application that either listens for exactly one incoming connection, or connects to a serversocket; next it prints everything it receives over that socket.
The situation where I connect to a serversocket (I use netcat with the -l option for this) works fine: everything netcat sends to my application is printed correctly. However, when I use my program to listen for that one incoming connection (generated by netcat), the connecting succeeds but then I get this runtime error:
QObject::connect: No such signal QThread::readyRead() in ..\QTcpTest\listener.cpp:8
Here is the entire code (don't mind the plain C I/O I use sometimes, I'll remove this later):
[ main.cpp ]
#include "peer.h"
#include <QCoreApplication>
#include <QThread>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static bool askIfServer();
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Peer *p;
int rval;
if (askIfServer())
p = new Peer;
else
p = new Peer(false);
rval = a.exec();
delete p;
return rval;
}
static bool askIfServer()
{
bool isServer;
char ibuf[BUFSIZ];
fputs("Choose \'host\' or \'join\': ", stderr);
fgets(ibuf, BUFSIZ, stdin);
ibuf[strlen(ibuf) - 1] = '\0';
fflush(stdin);
if (strcmp(ibuf, "host") == 0)
isServer = true;
else if (strcmp(ibuf, "join") == 0)
isServer = false;
else
{
fputs("Failure.\n", stderr);
exit(-1);
}
return isServer;
}
[ peer.h ]
#ifndef PEER_H
#define PEER_H
#include "listener.h"
#include <QObject>
#include <QThread>
#include <QtNetwork/QTcpSocket>
#include <QtNetwork/QTcpServer>
class Peer : public QObject
{
Q_OBJECT
public:
static const quint16 PORT = 5483;
explicit Peer(bool isHost = true, QString hostname = "localhost", QObject *parent = 0);
public slots:
void acceptPeer();
private:
bool _isHost;
QTcpServer *_server;
QTcpSocket *_socket;
QThread *_lThread;
Listener *_listener;
};
#endif // PEER_H
[ peer.cpp ]
#include "peer.h"
Peer::Peer(bool isHost, QString hostname, QObject *parent) :
QObject(parent),
_isHost(isHost)
{
if (_isHost)
{
_server = new QTcpServer;
connect(_server, SIGNAL(newConnection()), this, SLOT(acceptPeer()));
_server->listen(QHostAddress::Any, PORT);
}
else
{
_socket = new QTcpSocket;
connect(_socket, SIGNAL(hostFound()), this, SLOT(acceptPeer()));
_socket->connectToHost(hostname, PORT);
}
}
void Peer::acceptPeer()
{
disconnect(this, SLOT(acceptPeer()));
if (_isHost)
{
_socket = _server->nextPendingConnection();
delete _server;
_server = NULL;
}
_lThread = new QThread;
_listener = new Listener(_socket);
_listener->moveToThread(_lThread);
_lThread->start();
}
[ listener.h ]
#ifndef LISTENER_H
#define LISTENER_H
#include <iostream>
#include <QObject>
#include <QByteArray>
#include <QtNetwork/QTcpSocket>
using std::cout;
class Listener : public QObject
{
Q_OBJECT
public:
explicit Listener(QTcpSocket *socket, QObject *parent = 0);
public slots:
void acceptData();
private:
QTcpSocket *_socket;
};
#endif // LISTENER_H
[ listener.cpp ]
#include "listener.h"
Listener::Listener(QTcpSocket *socket, QObject *parent) :
QObject(parent),
_socket(socket)
{
// I guess this is where it goes wrong
connect(_socket, SIGNAL(readyRead()), this, SLOT(acceptData()), Qt::QueuedConnection);
}
void Listener::acceptData()
{
QByteArray data = _socket->readAll();
cout << data.constData();
}
The thing I do not get is that the error message claims that I try to connect some signal from QThread (and I clearly do not: see listener.cpp:8); even stranger is that this only happens when the socket is created by the QTcpServer instance, not in the other case. What am I missing?
EDIT: SOLVED
See my own answer.
I do not use an extra thread anymore (see comment). The problem was deleting the QTcpServer instance in Peer::acceptPeer(), once a connection was established. I thought that I could delete it because I only need that one socket QTcpServer::nextPendingConnection() returns. Apparently this is wrong, the socket is probably destroyed as well, leaving me with a rogue pointer (I think) crashing the program. Now I only call _server->pauseAccepting(); right after I get the socket, and all works fine.
Here is the change I've made:
void Peer::acceptPeer()
{
disconnect(this, SLOT(acceptPeer()));
if (_isHost)
{
_socket = _server->nextPendingConnection();
_server->pauseAccepting(); // no deletion
}
_listener = new Listener(_socket);
}

Qt Update after network reply

I am working with Qt creator to make a GUI program that takes in different URLs and will download and display the html code.
The user can add different URLs to a listWidget. Then the user can select a specific URL and download the html which will be displayed beside the list of URLs.
The problem I am having is getting the text area to update after the reply is received.
main.cpp - Basically just shows the window.
#include <QtGui/QApplication>
#include "mainwindow.h"
#include "htmlmanager.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindow.h - Pretty straight forward. Contains the object html that will be used to request the html from the inputted website.
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "htmlmanager.h"
#include <QString>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
HtmlManager html;
private slots:
void on_addButton_clicked();
void on_actionExit_triggered();
void on_removeButton_clicked();
void on_downloadButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp - This is the beginning of the problem. If you look down at the "downloadButton_clicked()" function, you see that it fetches the html by sending a request. However, the reply isn't recieved before the next line so the text field is set to "".
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_addButton_clicked()
{
if(!ui->lineEdit->text().isEmpty())
{
ui->listWidget->addItem(ui->lineEdit->text());
}
else
{
qDebug() << "Input field is empty.";
}
}
void MainWindow::on_actionExit_triggered()
{
//doesn't do anything right now
}
void MainWindow::on_removeButton_clicked()
{
if(ui->listWidget->currentItem()) //If an item is selected
{
delete ui->listWidget->currentItem();
}
else
{
qDebug() << "No selection";
}
}
void MainWindow::on_downloadButton_clicked()
{
if(ui->listWidget->currentItem()) //If an item is selected
{
html.fetch(ui->listWidget->currentItem()->text());
ui->textBrowser->setText(html.str);
}
else
{
qDebug() << "No selection";
}
}
htmlmaneger.h
#ifndef HTMLMANAGER_H
#define HTMLMANAGER_H
#include <QObject>
#include <QDebug>
#include <QtNetwork>
#include <QNetworkReply>
#include <QNetworkAccessManager>
#include <QString>
class HtmlManager : public QObject
{
Q_OBJECT
public:
HtmlManager();
void fetch(QString Url);
QString str;
public slots:
void replyFinished(QNetworkReply* pReply);
private:
QNetworkAccessManager* m_manager;
};
#endif // HTMLMANAGER_H
htmlmanager.cpp - Once the reply is received, it stores the html in the QString "str"
#include "htmlmanager.h"
HtmlManager::HtmlManager()
{
m_manager = new QNetworkAccessManager(this);
connect(m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
}
void HtmlManager::fetch(QString Url)
{
m_manager->get(QNetworkRequest(QUrl(Url)));
qDebug() << "Sending network request.";
}
void HtmlManager::replyFinished(QNetworkReply* pReply)
{
qDebug() << "Recieved network reply.";
QByteArray data=pReply->readAll();
str = data;
}
Is there an easy way to send the value of str to the MainWindow class once the reply is received, or is there a way for the onclick function to wait to update the text area until after a reply is received?
You definitely don't want to wait for a reply in your onClick() function. That will cause your program to be unresponsive until the network request comes it (which could very well take "forever").
One way to attack this would be to add a signal to your HtmlManager class. Something maybe called stringReceived. Then, in your mainwindow class you'd just need to add a line like this:
connect(html, SIGNAL(stringReceived(QString)), ui->textBrowser, SLOT(setText(QString));
QNetworkAccessManager don't provide synchronous or blocking approach. If you want to wait until reply has been received you have to go for waiting in a local event loop until reply finished signal invoked.
See the following link for turning asynchronous operation into synchronous one:
http://doc.qt.digia.com/qq/qq27-responsive-guis.html
In the section "Waiting in a Local Event Loop" they have shown an example using QNetworkAccessManager. You can use the same approach with timeout and local event loop.