Results 1 to 2 of 2

Thread: Access a member/widget of one class from a different class.

  1. #1
    Join Date
    Mar 2017
    Posts
    12
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Access a member/widget of one class from a different class.

    Hi I Have two different classes one is window whose base class is QWidget and another is socket whose base class is QObject. I have few widgets like reset and exit button and a text browser in main window. On other side I have a socket code which keeps on listening on a port. What I am trying to achieve is whenever a new client connects to the sever, display that client message on the mainwindow Textbrowser widget. But due to lack of knowledge in c++ as I am still learning it, I couldn't get this working. here is the code.
    here is main.cpp
    Qt Code:
    1. #include <QApplication>
    2. #include "window.h"
    3. #include "socket.h"
    4.  
    5. int main(int argc, char **argv)
    6. {
    7. QApplication app (argc, argv);
    8. Window window;
    9. Socket socket;
    10.  
    11. window.setStyleSheet("background-color: rgb(226, 226, 226);");
    12. window.showFullScreen();
    13. return app.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

    here is window.h
    Qt Code:
    1. #ifndef WINDOW_H
    2. #define WINDOW_H
    3.  
    4. #include <QWidget>
    5.  
    6. class QString;
    7.  
    8. class Window : public QWidget
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. explicit Window(QWidget *parent = 0);
    14.  
    15. QPushButton *helloButton;
    16. QPushButton *exitButton;
    17. QPushButton *resetButton;
    18. QTextBrowser *clientMsgWindow;
    19.  
    20. public slots:
    21. void reset();
    22. void setClientWindow(QString str);
    23. };
    24. #endif // WINDOW_H
    To copy to clipboard, switch view to plain text mode 

    this is window.cpp
    Qt Code:
    1. #include "window.h"
    2. #include <QPushButton>
    3. #include <QTextBrowser>
    4. #include <QDebug>
    5. #include <QString>
    6.  
    7. Window::Window(QWidget *parent) : QWidget(parent)
    8. {
    9. /****************** Hello BUTTON ********************/
    10. helloButton = new QPushButton(this);
    11. helloButton->setIconSize(QSize(145, 145));
    12. helloButton->setGeometry(15, 160, 145, 145);
    13. helloButton->setText("Hello World");
    14.  
    15. /******************reset BUTTON ********************/
    16. resetButton = new QPushButton(this);
    17. resetButton->setIconSize(QSize(145, 145));
    18. resetButton->setGeometry(15, 160, 145, 145);
    19. resetButton->setText("Click to Reset");
    20.  
    21. /************* EXIT BUTTON *********************/
    22. exitButton = new QPushButton(this);
    23. exitButton->setIcon(QIcon(":/new/prefix1/images/exit.png"));
    24. exitButton->setIconSize(QSize(145, 145));
    25. exitButton->setGeometry(635, 10, 145, 145);
    26. exitButton->setText("EXIT");
    27. //exitButton->setStyleSheet("background-color: rgb(236, 236, 236);");
    28. // Signal and slot for EXIT button
    29. qDebug() << connect(exitButton, SIGNAL (clicked()), this, SLOT (close()));
    30.  
    31. /*************** TEXT BROWSER *********************/
    32. clientMsgWindow = new QTextBrowser(this);
    33. clientMsgWindow->setMinimumSize(QSize(0,0));
    34. clientMsgWindow->setMaximumSize(QSize(10000,10000));
    35. clientMsgWindow->setGeometry(175, 50, 440, 420);
    36. clientMsgWindow->setStyleSheet("background-color: rgb(236, 236, 236);");
    37. }
    38.  
    39. void Window::setClientWindow(QString Str)
    40. {
    41. qDebug() << "Hit in Set client window to set Text";
    42. qDebug() << Str;
    43. clientMsgWindow->setText("This is the message from client");
    44. clientMsgWindow->setText(Str);
    45. qDebug() << "Setting text done";
    46. }
    47.  
    48. /**** Slot to reset the text browser **********/
    49. void Window::reset()
    50. {
    51. qDebug() << "Process in Reset Window";
    52. clientMsgWindow->clear();
    53.  
    54. }
    To copy to clipboard, switch view to plain text mode 

    this is socket.h
    Qt Code:
    1. #ifndef SOCKET_H
    2. #define SOCKET_H
    3.  
    4. #include <QObject>
    5. #include <QDebug>
    6. #include <QTcpServer>
    7. #include <QTcpSocket>
    8. #include <window.h>
    9.  
    10.  
    11. class Socket : public QObject
    12. {
    13. Q_OBJECT
    14. public:
    15. explicit Socket(QObject *parent = 0);
    16. void setWindow(Window *w);
    17. signals:
    18.  
    19. public slots:
    20. void newConnection();
    21.  
    22. private:
    23. QTcpServer *server;
    24. Window *window;
    25. };
    26.  
    27. #endif // SOCKET_H
    To copy to clipboard, switch view to plain text mode 

    this is socket.cpp
    Qt Code:
    1. #include "socket.h"
    2. #include "window.h"
    3. #include <QWidget>
    4. #include <QString>
    5.  
    6. Socket::Socket(QObject *parent):
    7. QObject(parent)
    8. {
    9. server = new QTcpServer(this);
    10. connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));
    11. if(!server->listen(QHostAddress::QHostAddress("192.168.0.1"), 5000)){
    12. qDebug() << "SERVER NOT STARTED";
    13. }
    14. else{
    15. qDebug() << "SERVER STARTED";
    16. }
    17. }
    18.  
    19. void Socket::newConnection()
    20. {
    21. QString clientMsg ;
    22. QTcpSocket *socket= server->nextPendingConnection();
    23. socket->write("Server Running on 192.168.0.1");
    24. socket->flush();
    25. socket->waitForBytesWritten();
    26.  
    27. // Recieve the data from Client
    28. socket->waitForReadyRead();
    29. qDebug() << (clientMsg = socket->readAll());
    30.  
    31. connect((socket, SIGNAL (readyRead)), Window::clientMsgWindow, SLOT(setClientWindow)); /*********** can I use signal and slot ************/
    32. //Window pWindow; /********* tried using this but GUI crash ************/
    33. //pWindow->setClientWindow(clientMsg);
    34. }
    To copy to clipboard, switch view to plain text mode 

    can I use SIGNAL&SLOT mechanism to set the recieved message on the txt browser(ClientMsgWindow). How so ever this connect gives error as /socket.cpp:31: error: object missing in reference to 'Window::clientMsgWindow' which points to the declaration of clientMsgWindow in window.h
    I am stuck in this.
    Last edited by Puneet2793; 16th May 2017 at 12:33.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Access a member/widget of one class from a different class.

    What is the answer you expect?
    Do you want us to post here the correct code for you?
    Then its us programming your app, not you.
    Your questions are basic knowledge that is available in documentation and tutorials and books and google can help you there.
    What would be the point for us to type all of it here again, aside the fact its not practical?

    Yes, you could use signals and slots for that.
    On how to use signals and slots, please read the documentation:
    http://doc.qt.io/qt-5/signalsandslots.html

    If you have any specific questions about things you need clarification about, feel free to ask.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Replies: 4
    Last Post: 16th June 2015, 18:50
  2. Access Signal of Class Member Variable
    By starkid in forum Newbie
    Replies: 7
    Last Post: 19th October 2011, 01:19
  3. Replies: 3
    Last Post: 12th April 2011, 11:58
  4. Replies: 4
    Last Post: 29th May 2010, 13:56
  5. Replies: 5
    Last Post: 14th July 2006, 23:42

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.