Results 1 to 11 of 11

Thread: Connecting two objects form another object

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2015
    Posts
    5
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Windows

    Default Connecting two objects form another object

    Hy

    I would like to ask someone for help. I wanted to make a gui server so I created a gui application and in main class I created my server (because if I created it in MainWindow class I had problems with incomingConnection). It was not a problem to connect server class with mainwindow class, but problem arises when I try to connect another object declared in server class with mainwindow, I do not know how to pass object address, so I ask for help. To further illustrate my problem I attached piece of code:

    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3. #include "server.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8. MainWindow w;
    9. w.show();
    10.  
    11. Server ServerTcp;
    12. QObject::connect(&ServerTcp,SIGNAL(SetInfo(QString)),&w,SLOT(GetInfo(QString)));
    13. ServerTcp.StartServer(&w);
    14.  
    15. return a.exec();
    16. }
    17.  
    18.  
    19.  
    20. #include "server.h"
    21. #include "client.h"
    22.  
    23. Server::Server(QObject *parent) :
    24. QTcpServer(parent)
    25. {
    26. }
    27.  
    28. void Server::StartServer(QMainWindow *object)
    29. {
    30. if(listen(QHostAddress::Any,1234)){
    31. obj = object;
    32. emit SetInfo("Started");
    33. }
    34. else{
    35. emit SetInfo("Not started!");
    36. }
    37. }
    38.  
    39. void Server::incomingConnection(int handle)
    40. {
    41. Client *clientT = new Client(this);
    42. connect(&clientT,SIGNAL(SetInfo(QString)),&obj,SLOT(GetInfo(QString)));
    43. clientT->SetSocket(handle);
    44. }
    To copy to clipboard, switch view to plain text mode 

    Thank you for reply

  2. #2
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Connecting two objects form another object

    If I understand what you're trying to do, shouldn't GetInfo be the signal (i.e. request to get info) and the slot be the SetInfo (i.e. set the info provided by the GetInfo signal)?

    Your cleintT variable is a local pointer, which will go out of scope as soon as your Server::incomingConnection method finishes. If you wish to later signal this object, you should save your clientT pointer as a member variable, then you can use to signal in your Server::StartServer method as follows (assuming member pointer is called m_clientT):

    Qt Code:
    1. void Server::StartServer(QMainWindow *object)
    2. {
    3. if(listen(QHostAddress::Any,1234)){
    4. obj = object;
    5. emit m_clientT->SetInfo("Started");
    6. }
    7. else{
    8. emit m_clientT->SetInfo("Not started!");
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    Is that what you are looking for?

    P.S. is obj a member variable or what's the purpose for obj = object?

Similar Threads

  1. public objects / good coding form
    By tooth in forum Newbie
    Replies: 4
    Last Post: 23rd November 2010, 23:22
  2. form and its objects communication
    By eva2002 in forum Qt Programming
    Replies: 9
    Last Post: 31st January 2010, 01:49
  3. Replies: 1
    Last Post: 7th August 2007, 08:27
  4. Connecting signal to form that is closing
    By steg90 in forum Qt Programming
    Replies: 3
    Last Post: 18th May 2007, 07:54
  5. Connecting slots/signals in subclassed form
    By qball2k5 in forum Qt Programming
    Replies: 2
    Last Post: 7th March 2006, 16:01

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.