There are two classes, let it be Form 1 and Form 2, which are created in Qt desinger. I want to tie them together. in the header file of Form 1 I connect the header of Form 2, I make the pointer Form2 *test; in the same form I create test = new Form2(); Everything works, now you can add a button and go to Form 2 or transfer any data from Form 1 to Form 2. BUT! If I want to connect Form 3 and add form 2 to the header and try to make a call to Form 2, I get an "incomprehensible type" error. Added additional class Form2; lines to header files class Form2; Everything compiles, but the program simply gives a critical error.

The scheme is this, if I move along the forms in one direction, then everything works.

1-> 2-> 3-> ..-> 10

If, I'm trying to move in different directions.

1-> 2-> 3-> 4-> 2 = Does not work.

1-> 2-> 3-> 1 = Does not work.

1-> 2-> 3-> 4-> 1 = Does not work.

  • Apparently, you forgot to transfer the addresses of the parents to the child objects. That is, parents have a pointer, but children do not, therefore, they can only be accessed down the hierarchy. - free_ze
  • Thanks for the answer, can you give a small example of adding addresses? - Renovacio
  • Pass a pointer to the parent via the constructor argument: сhildPtr = new MyForm (parentPtr); - free_ze
  • And, by the way, with 99% probability you inherit from QObject, in which object hierarchies are already implemented. You can read in the documentation - free_ze
  • Guys, please write a detailed answer. I didn’t even think that this is such a difficult problem “connection between forms”. It must somehow be done. - Renovacio

1 answer 1

For two-way data transfer between objects (especially between several objects), it is better not to use a pointer to a neighbor, but to use the signal and slot mechanism. With this connect you will need to do only in one window of the pair and there will be no need for cross-inclusions of header files.


Suppose we have two forms - Form1 and Form2. In both classes, we describe signals for sending data:

 signals: void sendData(QVariant variant); 

and data slots:

 public slots: voiв recieveData(QVariant variant); 

In the Form1 constructor, we connect the data send signals to the receiving slots (suppose that an object of the Form2 class is declared in the Form1 class and is called form2):

 connect(this, SIGNAL(sendData(QVariant), form2, SLOT(recieveData(QVariant)); //отправка данных из Form1 в Form2 connect(form2, SIGNAL(sendData(QVariant), this, SLOT(recieveData(QVariant)); //отправка данных обратно из Form2 в Form1 

Now, if we emit a signal with some data in Form1, the function of receiving data will be called in Form2: emit sendData (QVariant ("MyCoolData");

Conversely, to send data from Form2 to Form1, we emit a signal from Form2 in the same way.

Be careful that the emission of signals does not lead to an endless cyclical invocation of slots.

Connection of additional windows is done according to the same principle - we declare signals for sending and slots for receiving in both classes, making a connection in the one in which it is the "parent".

  • Can you please an example. How is this implemented in practice? - Renovacio
  • @Renovacio edited the answer, added an example - Bearded Beaver
  • Thank you very much! ) - Renovacio