PDA

View Full Version : Accessing GUI widget from different class



Mobility
4th December 2012, 21:02
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) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
sessionOpened();
}

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

void MainWindow::sessionOpened()
{
server.listen(QHostAddress::Any, quint16(555));
}

mainwindow.h

#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;

};

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)
: 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--;
}

Any help is highly appreciated! Thanks!

wysota
4th December 2012, 22:04
to update Qlabel that states how many connections are currently active (=how may threads are running currently).

Using a thread-per-network-connection semantics in Qt is Bad. You can handle all connections from within one thread and have a side effect of saving the time needed to solve your current problem.

Mobility
4th December 2012, 22:11
Is it possible to have several connections active and working simultaneously without using threads?

amleto
4th December 2012, 22:41
Off topic, but Op has circular reference => design error.

Main window composed of sslserver, and sslserver impl also needs full knowledge of MainWindow.

edit:
I think that main window code in your sslserver is just you being a bit confused. You want to be updating the existing mainwin nit creating a new one. So there you should emit a signal with the new count as an argument. Connect that signal to a slot on your mainwin, and from there you can update the label text

wysota
4th December 2012, 22:53
Is it possible to have several connections active and working simultaneously without using threads?

Yes, of course. It's just a matter of using signals and slots correctly.

Mobility
5th December 2012, 09:30
Off topic, but Op has circular reference => design error.

Main window composed of sslserver, and sslserver impl also needs full knowledge of MainWindow.

edit:
I think that main window code in your sslserver is just you being a bit confused. You want to be updating the existing mainwin nit creating a new one. So there you should emit a signal with the new count as an argument. Connect that signal to a slot on your mainwin, and from there you can update the label text

Thanks, I see your point. I already tried SIGNAL-SLOT solution, but I guess I had some error on it. I'll give it another try and get back with the result.

I think I should add following to the sslserver.c:


connect(thread, SIGNAL(finished()), main, SLOT(count_update()));

I'm not sure though what I should put to the third parameter? Certainly main is not correct. I think this was the problem I faced when I tried to use the SIGNALs for this issue.

Cheers!

amleto
5th December 2012, 10:15
sslserver should be emitting signal, therefore it should not be the one doing the connection.
mainwindow should connect to sslserver

Mobility
5th December 2012, 18:27
Thank guys! I got it working. My problem was that I was not using pointer of object sslserver. With the following connection it worked out:


SslServer *ptr_server = &server;
connect(ptr_server, SIGNAL(count(int)), this, SLOT(update_count(int)));

Cheers!

amleto
5th December 2012, 21:29
connect(&server, SIGNAL(count(int)), this, SLOT(update_count(int)));

that is good enough! ;)