Results 1 to 11 of 11

Thread: Connecting two objects form another object

  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?

  3. #3
    Join Date
    Mar 2015
    Posts
    5
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Connecting two objects form another object

    I already connected server class with mainwindow, I want to connect clientT class with mainwindow, so that get information out of clientT class, so as you see from code when I create server I call function startServer where I want to pass address of mainwindow, which I store as "QMainWindow *obj" as global variable in that class and than use this address in another function "incomingConnection" to connect newly formed class with mainwindow. SetInfo(QString) are signal that are named the same in server and client class.
    For example when someone connects to server I want that client class passed "Client connected" to mainwindow where text is displayed in QTextEdit widget. If it would be console app I would do qDebug << "Client connected", but because it is gui app there is this problem to pass data between objects, so basicaly I have three objects, two of them are defined in main.cpp and client object is locally defined in one those static objects. I know that when function finishes clientT will go out of scope, but just that brief I want this connection to work, and again when another incomingConnection comes, and so on till I close the program.


    Added after 11 minutes:


    Actually there is also missing disconnect.
    If I could only somehow pass address of mainwindow in this obj. Or is there some other way to resolve this problem of connecting two objects?

    Qt Code:
    1. void Server::incomingConnection(int handle)
    2. {
    3. Client *clientT = new Client(this);
    4. connect(&clientT,SIGNAL(SetInfo(QString)),&obj,SLOT(GetInfo(QString)));
    5. clientT->SetSocket(handle);
    6. disconnect(&clientT,SIGNAL(SetInfo(QString)),&obj,SLOT(GetInfo(QString)));
    7. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Doko; 30th March 2015 at 22:37.

  4. #4
    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

    So you pass the MainWindow address in StartServer method and save in the obj member variable, correct?

    Either pass the obj variable to the constructor of the Client or add a setter method to the client that can save the MainWindow address.

    Is that what you're looking for?

  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Connecting two objects form another object

    Quote Originally Posted by Doko View Post
    Qt Code:
    1. void Server::incomingConnection(int handle)
    2. {
    3. Client *clientT = new Client(this);
    4. connect(&clientT,SIGNAL(SetInfo(QString)),&obj,SLOT(GetInfo(QString)));
    5. clientT->SetSocket(handle);
    6. }
    To copy to clipboard, switch view to plain text mode 
    No & at client, this is already a pointer, same for obj.

    Quote Originally Posted by Doko View Post
    Actually there is also missing disconnect.
    Qt Code:
    1. void Server::incomingConnection(int handle)
    2. {
    3. Client *clientT = new Client(this);
    4. connect(&clientT,SIGNAL(SetInfo(QString)),&obj,SLOT(GetInfo(QString)));
    5. clientT->SetSocket(handle);
    6. disconnect(&clientT,SIGNAL(SetInfo(QString)),&obj,SLOT(GetInfo(QString)));
    7. }
    To copy to clipboard, switch view to plain text mode 
    If you disconnect right after connecting, why connect in the first place?

    Cheers,
    _

  6. #6
    Join Date
    Mar 2015
    Posts
    5
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Connecting two objects form another object

    Ok, I see it was really not good to disconnect right away.

    So you pass the MainWindow address in StartServer method and save in the obj member variable, correct?

    Either pass the obj variable to the constructor of the Client or add a setter method to the client that can save the MainWindow address.
    I understand, but I dont know how to do that. If I pass and than save MainWindow address in some object, I can not access MainWindow by that address.

    Simplified example:

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4. MainWindow w;
    5. w.show();
    6.  
    7. Server ServerTcp;
    8. ServerTcp.StartServer(&w); //is that the right way to send address??
    9.  
    10. return a.exec();
    11. }
    12.  
    13. #include "server.h"
    14. #include "mainwindow.h"
    15.  
    16. Server::Server(QObject *parent) :
    17. QTcpServer(parent)
    18. {
    19. }
    20.  
    21. void Server::StartServer(QMainWindow *object)
    22. {
    23. if(listen(QHostAddress::Any,1234)){
    24. obj = object;
    25. obj->GetInfo("Started"); //<--- ? Does not work (does not recognize GetInfo function)
    26. }
    27. else{
    28. obj->GetInfo("Not started!");
    29. }
    30. }
    To copy to clipboard, switch view to plain text mode 

    obj is declared in Server class under private: as QMainWindow *obj;
    why I can not access this function (void GetInfo(QString str) form server object, declared as public slot in MainWindow class)
    What am I doing wrong?

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Connecting two objects form another object

    Quote Originally Posted by Doko View Post
    obj is declared in Server class under private: as QMainWindow *obj;
    Quote Originally Posted by Doko View Post
    function (void GetInfo(QString str) form server object, declared as public slot in MainWindow class)
    What am I doing wrong?
    You have your answer right there.

  8. #8
    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

    Quote Originally Posted by Doko View Post
    obj is declared in Server class under private: as QMainWindow *obj;
    why I can not access this function (void GetInfo(QString str) form server object, declared as public slot in MainWindow class)
    What am I doing wrong?
    I see you are passing a QMainWindow* to Server::StartServer. Have you tried passing your MainWindow pointer instead?

    Please show the actual error. Compile error, run-time error, etc?

    Also post your MainWindow.h using [code][/code] tags please.
    Last edited by jefftee; 31st March 2015 at 15:13.

  9. #9
    Join Date
    Mar 2015
    Posts
    5
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Connecting two objects form another object

    I attached MainWindow.h, mainwindow.cpp and server.h

    I tried to do another simple program, I created mainwindow and in this object I created two dialogs that were declared with pointers (note that mainwindow is not declared with poiter, but as I understand is a object), so I passed address of the first dialog in the constructor of second dialog, everything else was the same as illustrated by code above and it worked perfectly, I was able to call a function of first dialog from the second dialog (example: dialog1->doSomething(), dialog1 was declader in second dialog), but I think that the issue is because mainwindow is not declared with pointer... I dont know how to do it differently.

    Issues:
    C:\Qt test\Control\server.cpp:13: error: 'class QMainWindow' has no member named 'GetInfo'
    obj->GetInfo("Started");
    ^
    C:\Qt test\Control\server.cpp:16: error: 'class QMainWindow' has no member named 'SetInfo'
    obj->SetInfo("Not started!");
    ^

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. class MainWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17.  
    18. public slots:
    19. void GetInfo(QString str);
    20.  
    21. private:
    22. Ui::MainWindow *ui;
    23. };
    24.  
    25.  
    26. #endif // MAINWINDOW_H
    27.  
    28. #include "mainwindow.h"
    29. #include "ui_mainwindow.h"
    30.  
    31. MainWindow::MainWindow(QWidget *parent) :
    32. QMainWindow(parent),
    33. ui(new Ui::MainWindow)
    34. {
    35. ui->setupUi(this);
    36. }
    37.  
    38. MainWindow::~MainWindow()
    39. {
    40. delete ui;
    41. }
    42.  
    43. void MainWindow::GetInfo(QString str)
    44. {
    45. ui->textEdit->append(str);
    46. }
    47.  
    48.  
    49. #ifndef SERVER_H
    50. #define SERVER_H
    51.  
    52. #include <QTcpServer>
    53. #include <QTcpSocket>
    54. #include <QAbstractSocket>
    55. #include <QMainWindow>
    56.  
    57. class Server : public QTcpServer
    58. {
    59. Q_OBJECT
    60. public:
    61. explicit Server(QObject *parent = 0);
    62. void StartServer(QMainWindow *object);
    63.  
    64. protected:
    65. void incomingConnection(int handle);
    66.  
    67. public slots:
    68.  
    69. private:
    70.  
    71. };
    72.  
    73. #endif // SERVER_H
    To copy to clipboard, switch view to plain text mode 

  10. #10
    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

    The error tells you exactly what the problem is. Your MainWindow inherits from QMainWindow, correct? So a MainWindow is a QMainWindow but a QMainWindow is *not* a MainWindow.

    The obj pointer you save needs to be a pointer to a MainWindow, not a QMainWindow.

    P.S. Look at the documentation for QMainWindow. Do you see a GetInfo method that returns void and has a QString as an argument? Nope, but your MainWindow class does.

  11. The following user says thank you to jefftee for this useful post:

    Doko (31st March 2015)

  12. #11
    Join Date
    Mar 2015
    Posts
    5
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Connecting two objects form another object

    Thank you very much. It was so obvious but I could not figure it out.

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.