Accessing GUI widget from different class
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
Code:
#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()
{
}
mainwindow.h
Code:
#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;
};
sslserver.c
Code:
#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--;
}
Any help is highly appreciated! Thanks!
Re: Accessing GUI widget from different class
Quote:
Originally Posted by
Mobility
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.
Re: Accessing GUI widget from different class
Is it possible to have several connections active and working simultaneously without using threads?
Re: Accessing GUI widget from different class
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
Re: Accessing GUI widget from different class
Quote:
Originally Posted by
Mobility
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.
Re: Accessing GUI widget from different class
Quote:
Originally Posted by
amleto
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:
Code:
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!
Re: Accessing GUI widget from different class
sslserver should be emitting signal, therefore it should not be the one doing the connection.
mainwindow should connect to sslserver
Re: Accessing GUI widget from different class
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:
Code:
SslServer *ptr_server = &server;
connect(ptr_server, SIGNAL(count(int)), this, SLOT(update_count(int)));
Cheers!
Re: Accessing GUI widget from different class
Code:
connect(&server, SIGNAL(count(int)), this, SLOT(update_count(int)));
that is good enough! ;)