Results 1 to 9 of 9

Thread: Accessing GUI widget from different class

  1. #1
    Join Date
    Sep 2012
    Posts
    34
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default 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
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QtGui>
    4. #include <QtNetwork>
    5. #include <cassert>
    6. #include <QDateTime>
    7. #include "sslserver.h"
    8. #include <stdlib.h>
    9.  
    10. MainWindow::MainWindow(QWidget *parent) :
    11. QMainWindow(parent),
    12. ui(new Ui::MainWindow)
    13. {
    14. ui->setupUi(this);
    15. sessionOpened();
    16. }
    17.  
    18. MainWindow::~MainWindow()
    19. {
    20. delete ui;
    21. }
    22.  
    23. void MainWindow::sessionOpened()
    24. {
    25. server.listen(QHostAddress::Any, quint16(555));
    26. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h
    Qt Code:
    1. #include <QMainWindow>
    2.  
    3. #include <QtNetwork>
    4. #include <QObject>
    5. #include <QTcpServer>
    6. #include <QTcpSocket>
    7. #include <QSslSocket>
    8. #include "sslserver.h"
    9.  
    10. namespace Ui {
    11. class MainWindow;
    12. }
    13.  
    14. class MainWindow : public QMainWindow
    15. {
    16. Q_OBJECT
    17.  
    18. public:
    19. explicit MainWindow(QWidget *parent = 0);
    20. ~MainWindow();
    21. void sessionOpened();
    22. Ui::MainWindow *ui;
    23.  
    24. public slots:
    25. void update_count();
    26.  
    27. private slots:
    28.  
    29.  
    30. private:
    31. QTcpServer *tcpServer;
    32. SslServer server;
    33.  
    34. };
    To copy to clipboard, switch view to plain text mode 

    sslserver.c
    Qt Code:
    1. #include "sslserver.h"
    2. #include "serverthread.h"
    3. #include "mainwindow.h"
    4. #include <stdlib.h>
    5. #include "ui_mainwindow.h"
    6.  
    7. int connection_count = 0;
    8.  
    9. SslServer::SslServer(QObject *parent)
    10. : QTcpServer(parent)
    11. {
    12. }
    13.  
    14. void SslServer::incomingConnection(int socketDescriptor)
    15. {
    16.  
    17. thread = new serverthread(socketDescriptor, this);
    18. connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    19. connect(thread, SIGNAL(finished()), thread, SLOT(count_delete()));
    20.  
    21. connection_count++;
    22. thread->start();
    23.  
    24. /*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... )*/
    25. MainWindow* main;
    26. main = new MainWindow();
    27. main->ui->setupUi(main);
    28. main->ui->label->setText("number of connections active currently...");
    29.  
    30. }
    31.  
    32. void SslServer::count_delete()
    33. {
    34. connection_count--;
    35. }
    To copy to clipboard, switch view to plain text mode 

    Any help is highly appreciated! Thanks!
    Last edited by Mobility; 4th December 2012 at 21:51.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Accessing GUI widget from different class

    Quote Originally Posted by Mobility View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Sep 2012
    Posts
    34
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Accessing GUI widget from different class

    Is it possible to have several connections active and working simultaneously without using threads?

  4. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default 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
    Last edited by amleto; 4th December 2012 at 22:52.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Accessing GUI widget from different class

    Quote Originally Posted by Mobility View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Sep 2012
    Posts
    34
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Accessing GUI widget from different class

    Quote Originally Posted by amleto View Post
    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:

    Qt Code:
    1. connect(thread, SIGNAL(finished()), main, SLOT(count_update()));
    To copy to clipboard, switch view to plain text mode 

    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!

  7. #7
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default 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
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  8. #8
    Join Date
    Sep 2012
    Posts
    34
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default 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:

    Qt Code:
    1. SslServer *ptr_server = &server;
    2. connect(ptr_server, SIGNAL(count(int)), this, SLOT(update_count(int)));
    To copy to clipboard, switch view to plain text mode 

    Cheers!

  9. #9
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Accessing GUI widget from different class

    Qt Code:
    1. connect(&server, SIGNAL(count(int)), this, SLOT(update_count(int)));
    To copy to clipboard, switch view to plain text mode 
    that is good enough!
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. accessing static member variables of one class in another class
    By jasonknight in forum General Programming
    Replies: 5
    Last Post: 6th September 2010, 15:53
  2. Accessing a class Object by pointer in another class
    By pdoria in forum Qt Programming
    Replies: 2
    Last Post: 14th January 2008, 16:59
  3. Not accessing widget outside the class
    By santosh.kumar in forum Qt Programming
    Replies: 6
    Last Post: 18th May 2007, 13:57
  4. Accessing to a static variable from the same class
    By xgoan in forum General Programming
    Replies: 6
    Last Post: 5th March 2007, 11:50
  5. accessing my own class-instances
    By mikro in forum Newbie
    Replies: 3
    Last Post: 11th July 2006, 01:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.