Results 1 to 14 of 14

Thread: Unable to set the value using the setter function

  1. #1
    Join Date
    Mar 2017
    Posts
    12
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Question Unable to set the value using the setter function

    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
    Qt Code:
    1. Here is the code for main.cpp
    2. #include <QApplication>
    3. #include "window.h"
    4. #include "bbbserver.h"
    5.  
    6. int main(int argc, char **argv)
    7. {
    8. QApplication app (argc, argv);
    9.  
    10. bbbServer server;
    11. Window window;
    12.  
    13. window.setStyleSheet("background-color: rgb(226, 226, 226);");
    14. window.showFullScreen();
    15.  
    16. return app.exec();
    17. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. Here is the code for window.h
    2. #ifndef WINDOW_H
    3. #define WINDOW_H
    4.  
    5. #include <QWidget>
    6.  
    7. class QProcess;
    8. class QFile;
    9.  
    10. class Window : public QWidget
    11. {
    12. Q_OBJECT
    13. public:
    14.  
    15. explicit Window(QWidget *parent = 0);
    16.  
    17. void setStatusWindow(QString str);
    18. QTextBrowser *statusWindow;
    19.  
    20.  
    21. private slots:
    22.  
    23. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. Here is window.cpp
    2. #include "window.h"
    3. #include <QPushButton>
    4. #include <QProcess>
    5. #include <QTextBrowser>
    6. #include <QDebug>
    7.  
    8. Window::Window(QWidget *parent) : QWidget(parent)
    9. {
    10. // Create and position the buttons on the main window
    11.  
    12.  
    13. /*************** text browser *********************/
    14. statusWindow = new QTextBrowser(this);
    15. statusWindow->setMinimumSize(QSize(0,0));
    16. statusWindow->setMaximumSize(QSize(10000,10000));
    17. statusWindow->setGeometry(175, 50, 440, 420);
    18. statusWindow->setStyleSheet("background-color: rgb(236, 236, 236);");
    19. }
    20.  
    21. void Window::setStatusWindow(QString str)
    22. {
    23. qDebug() << "Hit in Setter function to set Text";
    24. statusWindow->setText(str);
    25.  
    26. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. and here is the bbbserver.cpp
    2. #include "bbbserver.h"
    3. #include "window.h"
    4.  
    5. bbbServer::bbbServer(QObject *parent):
    6. QObject(parent)
    7. {
    8.  
    9.  
    10. /*************************** SERVER *********************************/
    11. server = new QTcpServer(this);
    12. connect(server, SIGNAL(newConnection()), this, SLOT(newConnection()));
    13.  
    14. if(!server->listen(QHostAddress::QHostAddress("192.168.0.1"), 5000))
    15. {
    16. qDebug() << "SERVER NOT STARTED";
    17. }
    18. else
    19. {
    20. qDebug() << "SERVER STARTED";
    21. }
    22. }
    23.  
    24. void bbbServer::newConnection()
    25. {
    26. QTcpSocket *socket= server->nextPendingConnection();
    27. socket->write("Connection from 192.168.0.1 BBB\n");
    28. socket->flush();
    29. socket->waitForBytesWritten(30000);
    30.  
    31. socket->waitForReadyRead(30000);
    32. qDebug() << (clientMsg = socket->readAll());
    33.  
    34. // creating an instance of window to change Euid text Browser
    35. Window window;
    36. window.setStatusWindow("clientMsg");
    37. }
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Unable to set the value using the setter function

    Add to the debug output also the input argument 'str' to make sure you are not setting an empty string.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Mar 2017
    Posts
    12
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Unable to set the value using the setter function

    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.
    Last edited by Puneet2793; 15th May 2017 at 11:43.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Unable to set the value using the setter function

    Your Window is destroyed right after you set the text:
    Qt Code:
    1. void bbbServer::newConnection()
    2. {
    3. QTcpSocket *socket= server->nextPendingConnection();
    4. socket->write("Connection from 192.168.0.1 BBB\n");
    5. socket->flush();
    6. socket->waitForBytesWritten(30000);
    7.  
    8. socket->waitForReadyRead(30000);
    9. qDebug() << (clientMsg = socket->readAll());
    10.  
    11. // creating an instance of window to change Euid text Browser
    12. 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.
    13.  
    14. window.setStatusWindow("clientMsg");
    15. //Here your window gets destroyed.
    16. }
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Mar 2017
    Posts
    12
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Unable to set the value using the setter function

    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
    Last edited by Puneet2793; 15th May 2017 at 12:58.

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Unable to set the value using the setter function

    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:
    Qt Code:
    1. oid bbbServer::newConnection()
    2. {
    3. QTcpSocket *socket= server->nextPendingConnection();
    4. socket->write("Connection from 192.168.0.1 BBB\n");
    5. socket->flush();
    6. socket->waitForBytesWritten(30000);
    7.  
    8. socket->waitForReadyRead(30000);
    9. qDebug() << (clientMsg = socket->readAll());
    10.  
    11. // creating an instance of window to change Euid text Browser
    12. Window* pWindow = new Windows(this);
    13.  
    14. pWindow->setStatusWindow("clientMsg");
    15.  
    16. }
    To copy to clipboard, switch view to plain text mode 

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Mar 2017
    Posts
    12
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Unable to set the value using the setter function

    @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.
    Last edited by Puneet2793; 16th May 2017 at 07:41.

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Unable to set the value using the setter function

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Mar 2017
    Posts
    12
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Unable to set the value using the setter function

    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.

  10. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Unable to set the value using the setter function

    Please use one thread for the same set of issues.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  11. #11
    Join Date
    Mar 2017
    Posts
    12
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Unable to set the value using the setter function

    can't edit it.

  12. #12
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Unable to set the value using the setter function

    One thread, not post.
    Simply post a new post in this thread.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  13. #13
    Join Date
    Mar 2017
    Posts
    12
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Unable to set the value using the setter function

    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"
    Last edited by Puneet2793; 16th May 2017 at 13:24.

  14. #14
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Unable to set the value using the setter function

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Replies: 11
    Last Post: 5th September 2012, 20:47
  2. Replies: 8
    Last Post: 24th February 2011, 13:22
  3. Why my setter don't work?
    By gboelter in forum Newbie
    Replies: 7
    Last Post: 8th November 2009, 21:27
  4. [QtScript] One getter/setter function for multiple properties
    By supergillis in forum Qt Programming
    Replies: 3
    Last Post: 22nd May 2009, 09:52
  5. Replies: 2
    Last Post: 28th April 2007, 09:02

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.