PDA

View Full Version : Unable to set the value using the setter function



Puneet2793
15th May 2017, 10:55
Hi I have two different classes A and B files. I am trying to access the object of class A from B. The function gets called i can see via the Debug messages but It doesn't set the textbrowser with the string I passed. Here is the code for Main window


Here is the code for main.cpp
#include <QApplication>
#include "window.h"
#include "bbbserver.h"

int main(int argc, char **argv)
{
QApplication app (argc, argv);

bbbServer server;
Window window;

window.setStyleSheet("background-color: rgb(226, 226, 226);");
window.showFullScreen();

return app.exec();
}


Here is the code for window.h
#ifndef WINDOW_H
#define WINDOW_H

#include <QWidget>

class QPushButton;
class QTextBrowser;
class QProcess;
class QFile;

class Window : public QWidget
{
Q_OBJECT
public:

explicit Window(QWidget *parent = 0);

void setStatusWindow(QString str);
QTextBrowser *statusWindow;


private slots:

};




Here is window.cpp
#include "window.h"
#include <QPushButton>
#include <QProcess>
#include <QTextBrowser>
#include <QDebug>

Window::Window(QWidget *parent) : QWidget(parent)
{
// Create and position the buttons on the main window


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

void Window::setStatusWindow(QString str)
{
qDebug() << "Hit in Setter function to set Text";
statusWindow->setText(str);

}



and here is the bbbserver.cpp
#include "bbbserver.h"
#include "window.h"

bbbServer::bbbServer(QObject *parent):
QObject(parent)
{


/*************************** SERVER *********************************/
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 bbbServer::newConnection()
{
QTcpSocket *socket= server->nextPendingConnection();
socket->write("Connection from 192.168.0.1 BBB\n");
socket->flush();
socket->waitForBytesWritten(30000);

socket->waitForReadyRead(30000);
qDebug() << (clientMsg = socket->readAll());

// creating an instance of window to change Euid text Browser
Window window;
window.setStatusWindow("clientMsg");
}

The problem here is whenever I send a message from the client side. I get the string and debug messages that code is hitting in setter function but I doesn't set the value of Status Window. Whats wrong there?

high_flyer
15th May 2017, 12:05
Add to the debug output also the input argument 'str' to make sure you are not setting an empty string.

Puneet2793
15th May 2017, 12:17
Yes the input string is not empty verified from qDebug messages.

true
true
true
true
"Connected to server"
true
true
true
true
Hit in Setter function to set Text
"Connected to server"

These 4 true messages are from the window.cpp in which I have connected some widgets with slots. But that part is intact from this. And I am not sure why I get these true messages each time.

high_flyer
15th May 2017, 13:26
Your Window is destroyed right after you set the text:


void bbbServer::newConnection()
{
QTcpSocket *socket= server->nextPendingConnection();
socket->write("Connection from 192.168.0.1 BBB\n");
socket->flush();
socket->waitForBytesWritten(30000);

socket->waitForReadyRead(30000);
qDebug() << (clientMsg = socket->readAll());

// creating an instance of window to change Euid text Browser
Window window; //<<<<----- this is a local variable that gets destroyed at the end of the methods scope, try allocating on the heap using a pointer.

window.setStatusWindow("clientMsg");
//Here your window gets destroyed.
}

Puneet2793
15th May 2017, 13:41
If I take a pointer as

Window *window;
window->setStatusWindow("clientMsg");

My main GUI gets killed by a signal as soon as client connects and send message to the server(Qt), Here is the debug messages


SERVER STARTED
true
true
true
true
"Connected to server"
Hit in Setter function to set Text
"Connected to server"
Process killed by signal

high_flyer
15th May 2017, 15:03
You really need to get basic C/C++ knowledge first.
You can't write a book without first knowing how to write the language and its grammar either.

You have to initializes the pointer of course:


oid bbbServer::newConnection()
{
QTcpSocket *socket= server->nextPendingConnection();
socket->write("Connection from 192.168.0.1 BBB\n");
socket->flush();
socket->waitForBytesWritten(30000);

socket->waitForReadyRead(30000);
qDebug() << (clientMsg = socket->readAll());

// creating an instance of window to change Euid text Browser
Window* pWindow = new Windows(this);

pWindow->setStatusWindow("clientMsg");

}


Please note that the above code only addresses the problem of your variable getting to end of scope.
The way its implemented above is still bad code.
You need to manage your Window instances.
newConnection() is called for every new connection, so each time you get a new connection you also create a new Window.
The code above is not tracking the various Window instances, which you might want to do depending on what you do with these instances later on.
Parenting them as I did in the code above at least guarantees you that they will get destroyed and not leak when their parent is destroyed.

Puneet2793
16th May 2017, 08:33
@high_flyer this also didn't worked. Yes I need to learn c++ not very much familiar with this. Undergoing the learning process. If I use the code which you gave above, the program hits in the member function to set textbrowser but after that it terminates the GUI.

high_flyer
16th May 2017, 11:18
As I said, my solutions only solved a specific problem (or rather, its not a solution but a work around, a hack).
Now that I have looked a bit closer in to your code there is a lot which is wrong.
For example you are initializing a Window in you main() and then again in bbbServer::newConnection() which I think you don't really want, that alone could be a crash reason.
If I try to derive your intention from your main() then you need to pass your Window from main to your server somehow, or, get the information from your server to your Window somehow.
At the moment you are initializing two separate Window instances which have nothing to do with each other.
There is so much basic stuff which is wrong, its hard to help you in a meaningful way.
You really have to know basic C++ before your can code in Qt.

Puneet2793
16th May 2017, 11:30
Yes, I have changed the code little bit. Shall I post it in a new thread??? What I have is one main windown which have widgets like Textbrowser, exit button, and reset button. And then there is a socket which keeps on listening , once there is a new connection. I read the string in socket code and then set it on textbrowser which is in main window.

high_flyer
16th May 2017, 11:49
Please use one thread for the same set of issues.

Puneet2793
16th May 2017, 12:06
can't edit it.

high_flyer
16th May 2017, 13:35
One thread, not post.
Simply post a new post in this thread.

Puneet2793
16th May 2017, 14:12
After reading the Signal and slots, I tried this way to connect the signal from server with SLOT on window.


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());

qDebug() << QObject::connect(socket, SIGNAL(readyRead()), this ,SLOT(Window::setClientWindow())) ;
}


but when i run the client code from another system I get a message like "Object::connect: No such slot Socket::Window::setClientWindow() in ../guitest/socket.cpp:31"

high_flyer
16th May 2017, 15:51
You probably need to read again how to use the connect() method.
here is the signature of connect():
http://doc.qt.io/qt-5/qobject.html#connect
now compare it to your usage of connect, specifically your SLOT() parameter.