Hi there,
I'm working on TCP (ssl) server with threads. GUI is set up in mainwindow, but I would need to update some of the GUI widgets from different class for example to update Qlabel that states how many connections are currently active (=how may threads are running currently). I have tried different things and found couple of posts with similar problems, but none of them did help me to solve this. I put the key points of the code here if you guys can help me.
mainwindow.c
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtGui>
#include <QtNetwork>
#include <cassert>
#include <QDateTime>
#include "sslserver.h"
#include <stdlib.h>
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
sessionOpened();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::sessionOpened()
{
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtGui>
#include <QtNetwork>
#include <cassert>
#include <QDateTime>
#include "sslserver.h"
#include <stdlib.h>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
sessionOpened();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::sessionOpened()
{
server.listen(QHostAddress::Any, quint16(555));
}
To copy to clipboard, switch view to plain text mode
mainwindow.h
#include <QMainWindow>
#include <QtNetwork>
#include <QObject>
#include <QTcpServer>
#include <QTcpSocket>
#include <QSslSocket>
#include "sslserver.h"
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
void sessionOpened();
Ui::MainWindow *ui;
public slots:
void update_count();
private slots:
private:
SslServer server;
};
#include <QMainWindow>
#include <QtNetwork>
#include <QObject>
#include <QTcpServer>
#include <QTcpSocket>
#include <QSslSocket>
#include "sslserver.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void sessionOpened();
Ui::MainWindow *ui;
public slots:
void update_count();
private slots:
private:
QTcpServer *tcpServer;
SslServer server;
};
To copy to clipboard, switch view to plain text mode
sslserver.c
#include "sslserver.h"
#include "serverthread.h"
#include "mainwindow.h"
#include <stdlib.h>
#include "ui_mainwindow.h"
int connection_count = 0;
SslServer
::SslServer(QObject *parent
){
}
void SslServer::incomingConnection(int socketDescriptor)
{
thread = new serverthread(socketDescriptor, this);
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
connect(thread, SIGNAL(finished()), thread, SLOT(count_delete()));
connection_count++;
thread->start();
/*Following is my latest trial which does not work. I have also tried SIGNAL-SLOT solution, but that did not work out (either that's not the way to go, or didn't make it correctly... )*/
MainWindow* main;
main = new MainWindow();
main->ui->setupUi(main);
main->ui->label->setText("number of connections active currently...");
}
void SslServer::count_delete()
{
connection_count--;
}
#include "sslserver.h"
#include "serverthread.h"
#include "mainwindow.h"
#include <stdlib.h>
#include "ui_mainwindow.h"
int connection_count = 0;
SslServer::SslServer(QObject *parent)
: QTcpServer(parent)
{
}
void SslServer::incomingConnection(int socketDescriptor)
{
thread = new serverthread(socketDescriptor, this);
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
connect(thread, SIGNAL(finished()), thread, SLOT(count_delete()));
connection_count++;
thread->start();
/*Following is my latest trial which does not work. I have also tried SIGNAL-SLOT solution, but that did not work out (either that's not the way to go, or didn't make it correctly... )*/
MainWindow* main;
main = new MainWindow();
main->ui->setupUi(main);
main->ui->label->setText("number of connections active currently...");
}
void SslServer::count_delete()
{
connection_count--;
}
To copy to clipboard, switch view to plain text mode
Any help is highly appreciated! Thanks!
Bookmarks