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)
{
Window window;
Socket socket;
window.setStyleSheet("background-color: rgb(226, 226, 226);");
window.showFullScreen();
return app.exec();
}
#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();
}
To copy to clipboard, switch view to plain text mode
here is window.h
#ifndef WINDOW_H
#define WINDOW_H
#include <QWidget>
{
Q_OBJECT
public:
explicit Window
(QWidget *parent
= 0);
public slots:
void reset();
};
#endif // 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
To copy to clipboard, switch view to plain text mode
this is window.cpp
#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();
}
#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();
}
To copy to clipboard, switch view to plain text mode
this is socket.h
#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
#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
To copy to clipboard, switch view to plain text mode
this is socket.cpp
#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);
}
#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);
}
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.
Bookmarks