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
Code:
#include <QApplication>
#include "window.h"
#include "socket.h"
int main(int argc, char **argv)
{
Window window;
Socket socket;
window.setStyleSheet("background-color: rgb(226, 226, 226);");
window.showFullScreen();
return app.exec();
}
here is window.h
Code:
#ifndef WINDOW_H
#define WINDOW_H
#include <QWidget>
{
Q_OBJECT
public:
explicit Window
(QWidget *parent
= 0);
public slots:
void reset();
};
#endif // WINDOW_H
this is window.cpp
Code:
#include "window.h"
#include <QPushButton>
#include <QTextBrowser>
#include <QDebug>
#include <QString>
{
/****************** Hello BUTTON ********************/
helloButton
->setIconSize
(QSize(145,
145));
helloButton->setGeometry(15, 160, 145, 145);
helloButton->setText("Hello World");
/******************reset BUTTON ********************/
resetButton
->setIconSize
(QSize(145,
145));
resetButton->setGeometry(15, 160, 145, 145);
resetButton->setText("Click to Reset");
/************* EXIT BUTTON *********************/
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
->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
Code:
#ifndef SOCKET_H
#define SOCKET_H
#include <QObject>
#include <QDebug>
#include <QTcpServer>
#include <QTcpSocket>
#include <window.h>
{
Q_OBJECT
public:
explicit Socket
(QObject *parent
= 0);
void setWindow(Window *w);
signals:
public slots:
void newConnection();
private:
Window *window;
};
#endif // SOCKET_H
this is socket.cpp
Code:
#include "socket.h"
#include "window.h"
#include <QWidget>
#include <QString>
{
connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));
qDebug() << "SERVER NOT STARTED";
}
else{
qDebug() << "SERVER STARTED";
}
}
void Socket::newConnection()
{
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.
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.