I am a newbie to QT and C++. I am stuck at a problem where I have two different classes and from B.cpp I want to access a variable of A.cpp. Using debug messages I have seen that the code hits in the setter function but it never sets the value of Textbrowser.
Here is the code of main.cpp.
#include <QApplication>
#include "window.h"
#include "socket.h"
int main(int argc, char **argv)
{
QApplication app (argc, argv);
Window *window = new Window();
Socket *socket = new 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
Here 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->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();
}
Here is the socket.h
#ifndef SOCKET_H
#define SOCKET_H
#include <QObject>
#include <QDebug>
#include <QTcpServer>
#include <QTcpSocket>
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
Here is the 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.2.1"), 2793)){
qDebug() << "SERVER NOT STARTED";
}
else{
qDebug() << "SERVER STARTED";
}
}
void Socket::setWindow(Window *w)
{
this->window = w;
}
void Socket::newConnection()
{
QString clientMsg ;
QTcpSocket *socket= server->nextPendingConnection();
socket->write("Server Running on 192.168.2.1");
socket->flush();
socket->waitForBytesWritten();
// Recieve the data from Client
socket->waitForReadyRead();
qDebug() << (clientMsg = socket->readAll());
// Set the Textbrowser clientMsgWindow
this->window->setClientWindow(clientMsg);
}
The GUI terminates each time client sends a message.