Results 1 to 9 of 9

Thread: Accessing GUI widget from different class

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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.