PDA

View Full Version : Access a member/widget of one class from a different class.



Puneet2793
16th May 2017, 11:04
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


#include <QApplication>
#include "window.h"
#include "socket.h"

int main(int argc, char **argv)
{
QApplication app (argc, argv);
Window window;
Socket socket;

window.setStyleSheet("background-color: rgb(226, 226, 226);");
window.showFullScreen();
return app.exec();
}


here is window.h


#ifndef WINDOW_H
#define WINDOW_H

#include <QWidget>

class QPushButton;
class QTextBrowser;
class QString;

class Window : public QWidget
{
Q_OBJECT

public:
explicit Window(QWidget *parent = 0);

QPushButton *helloButton;
QPushButton *exitButton;
QPushButton *resetButton;
QTextBrowser *clientMsgWindow;

public slots:
void reset();
void setClientWindow(QString str);
};
#endif // WINDOW_H


this is window.cpp


#include "window.h"
#include <QPushButton>
#include <QTextBrowser>
#include <QDebug>
#include <QString>

Window::Window(QWidget *parent) : QWidget(parent)
{
/****************** Hello BUTTON ********************/
helloButton = new QPushButton(this);
helloButton->setIconSize(QSize(145, 145));
helloButton->setGeometry(15, 160, 145, 145);
helloButton->setText("Hello World");

/******************reset BUTTON ********************/
resetButton = new QPushButton(this);
resetButton->setIconSize(QSize(145, 145));
resetButton->setGeometry(15, 160, 145, 145);
resetButton->setText("Click to Reset");

/************* EXIT BUTTON *********************/
exitButton = new QPushButton(this);
exitButton->setIcon(QIcon(":/new/prefix1/images/exit.png"));
exitButton->setIconSize(QSize(145, 145));
exitButton->setGeometry(635, 10, 145, 145);
exitButton->setText("EXIT");
//exitButton->setStyleSheet("background-color: rgb(236, 236, 236);");
// Signal and slot for EXIT button
qDebug() << connect(exitButton, SIGNAL (clicked()), this, SLOT (close()));

/*************** TEXT BROWSER *********************/
clientMsgWindow = new QTextBrowser(this);
clientMsgWindow->setMinimumSize(QSize(0,0));
clientMsgWindow->setMaximumSize(QSize(10000,10000));
clientMsgWindow->setGeometry(175, 50, 440, 420);
clientMsgWindow->setStyleSheet("background-color: rgb(236, 236, 236);");
}

void Window::setClientWindow(QString Str)
{
qDebug() << "Hit in Set client window to set Text";
qDebug() << Str;
clientMsgWindow->setText("This is the message from client");
clientMsgWindow->setText(Str);
qDebug() << "Setting text done";
}

/**** Slot to reset the text browser **********/
void Window::reset()
{
qDebug() << "Process in Reset Window";
clientMsgWindow->clear();

}


this is socket.h


#ifndef SOCKET_H
#define SOCKET_H

#include <QObject>
#include <QDebug>
#include <QTcpServer>
#include <QTcpSocket>
#include <window.h>

class QTextBrowser;

class Socket : public QObject
{
Q_OBJECT
public:
explicit Socket(QObject *parent = 0);
void setWindow(Window *w);
signals:

public slots:
void newConnection();

private:
QTcpServer *server;
Window *window;
};

#endif // SOCKET_H



this is socket.cpp


#include "socket.h"
#include "window.h"
#include <QWidget>
#include <QString>

Socket::Socket(QObject *parent):
QObject(parent)
{
server = new QTcpServer(this);
connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));
if(!server->listen(QHostAddress::QHostAddress("192.168.0.1"), 5000)){
qDebug() << "SERVER NOT STARTED";
}
else{
qDebug() << "SERVER STARTED";
}
}

void Socket::newConnection()
{
QString clientMsg ;
QTcpSocket *socket= server->nextPendingConnection();
socket->write("Server Running on 192.168.0.1");
socket->flush();
socket->waitForBytesWritten();

// Recieve the data from Client
socket->waitForReadyRead();
qDebug() << (clientMsg = socket->readAll());

connect((socket, SIGNAL (readyRead)), Window::clientMsgWindow, SLOT(setClientWindow)); /*********** can I use signal and slot ************/
//Window pWindow; /********* tried using this but GUI crash ************/
//pWindow->setClientWindow(clientMsg);
}


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.

high_flyer
16th May 2017, 12:48
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.