Study on the transfer directionality of QT signal and slot mechanism (conclusion is wrong, but can look at the analysis process)

Source: Internet
Author: User

QT has been studying recently due to the needs of the project. Signal and slot mechanism is a feature of QT, which allows the transfer of parameters between the two, in order to achieve communication between the objects. This parameter is present in the parameter list of the signal and in the parameter list of the slot function, respectively. It is important to note that if you bind a slot function to a signal, the number of parameter list elements of the slot function can be less than the number of parameter list elements that are equal to the signal. and the order and type cannot be changed. The missing parameters should be missing from the tail of the signal parameter.

Suddenly today I think of a problem, if an object signals, the internal member variable (non-simple type) as a parameter sent out, the slot function can receive this object, then the slot function can fully manipulate the object? If it can be fully operated, then this signal and groove mechanism is a two-way communication process, that a can trigger b,b and can operate on a, if the operation is not valid, then the mechanism is a one-way communication process, that is, a trigger b,b processing, but cannot reverse write data.

Gossip, let the instance speak

First set up a console application

Creating a header file CustomClasses.h

The code is as follows:

[CPP]View PlainCopy
  1. #ifndef Customclasses_h
  2. #define Customclasses_h
  3. #include <QObject>
  4. Class Tperson
  5. {
  6. Private
  7. QString _name;
  8. Public
  9. Tperson ();
  10. void SetName (QString Name);
  11. QString GetName ();
  12. };
  13. Class Tsender: Publicqobject
  14. {
  15. Q_object
  16. Private
  17. Tperson _p;
  18. Public
  19. Tsender ();
  20. void Click ();
  21. void disp ();
  22. Signals:
  23. void NewName (Tperson P);
  24. };
  25. Class Treceiver: Publicqobject
  26. {
  27. Q_object
  28. Public
  29. Treceiver ();
  30. Public Slots:
  31. void Getnewname (Tperson P);
  32. };
  33. #endif//Customclasses_h

Three classes of Tperson, Tsender, and treceiver are defined here.

Tperson is a very simple class that contains a private member, _name, with a get and set method, respectively.

The Tsender class contains a private Tperson instance _p,click () method that simulates a button click and emits a newname (Tperson p) signal when the Click () method is executed.

The Treceiver class contains a getnewname (Tperson p) slot for receiving newname (Tperson p) signals from Tsender.

Establish the corresponding CustomClasses.cpp

The code is as follows:

[CPP]View PlainCopy
  1. #include "CustomClasses.h"
  2. #include <QtCore>
  3. #include <QObject>
  4. Tperson::tperson ()
  5. {
  6. //  
  7. }
  8. void Tperson::setname (QString Name)
  9. {
  10. this->_name=name;
  11. }
  12. QString Tperson::getname ()
  13. {
  14. return this->_name;
  15. }
  16. Tsender::tsender ()
  17. {
  18. this->_p.setname ("Jack");
  19. }
  20. void Tsender::click ()
  21. {
  22. Qdebug () <<"Now begin to send the person";
  23. Emit This->newname (this->_p);
  24. Qdebug () <<"person with sended";
  25. }
  26. void Tsender::d ISP ()
  27. {
  28. Qdebug () <<"Current person name is:" <<this->_p.getname ();
  29. }
  30. Treceiver::treceiver ()
  31. {
  32. //  
  33. }
  34. void Treceiver::getnewname (Tperson P)
  35. {
  36. Qdebug () <<"Receive a person name is:" <<p.getname ();
  37. Qdebug () <<"begin to change name";
  38. P.setname ("Rose");
  39. Qdebug () <<"Finish to change name";
  40. }

This file is the implementation of the above content, but it is worth noting that in the implementation of void treceiver::getnewname (Tperson p), the P parameter is manipulated, and the SetName method is called.

Master File main.cpp

The code is as follows:

[CPP]View PlainCopy
  1. #include <QtCore/QCoreApplication>
  2. #include <QtCore>
  3. #include <QObject>
  4. #include "CustomClasses.h"
  5. int main (int argc, char *argv[])
  6. {
  7. Qcoreapplication A (argc, argv);
  8. Tsender send;
  9. Treceiver recv;
  10. Qobject::connect (&send, SIGNAL (NewName (Tperson)), &recv, SLOT (Getnewname (Tperson)));
  11. Send.disp ();
  12. Send.click ();
  13. Send.disp ();
  14. return a.exec ();
  15. }

The code instantiates the Tsender class and the Treceiver class, and binds the newname signal to the Getnewname slot. The code is simple, and the rest is no longer introduced.

Let's look at the running results:

Let's analyze the program. After the send instance and the Recv instance have been declared, the related events are bound. First, look at the Name property of the Tperson class instance _p in the current send object. The first line shows the initial jack, and then calls the Send's click () method, which emits the newname signal, which is received by the Recv Getnewname slot function, which displays the name value of the object currently received. It then calls the SetName method of the receiving object, setting the name to "Rose". After the slot function is executed, the code jumps to the position where the NewName signal is triggered, i.e. the "Emit this->newname (this->_p)" of the Click () method; "Back; the _p instance inside the Send Instance Name property is displayed at the end. It was observed that although the Name property was reset in the slot function, it did not change the attribute value of the signal initiator instance (still jack). So we can say that the signal of this transitive object is one-way communication with the slot mechanism. The direction of delivery is the signal sender to the receiver. Of course, this example is just a passing object, I experimented, if the above code to make minor changes, the transfer of parameters into the object pointer, it will eventually affect the data sent by the signal. As shown in the following:

That is, if you are passing pointers, the mechanism is two-way communication, and the key is to see how to use them. I personally do not recommend using pointer delivery. Because a signal can be bound to more than one slot function, if one of the data has been modified, it will affect the subsequent slot function execution, at this time the parameter state is unknown, is not easy to maintain. Of course, nothing is absolute, except under special circumstances.

Add another two points:

First: The term "slot function" mentioned above is not appropriate. Because the method cannot contain a return value. Must be void, so called "groove process" is more appropriate, but has widely accepted the term, so there is no drill down.

Second: At the beginning, for the sake of diagram, the definition and implementation of the three classes of Tperson, Tsender and Treceiver are put into main.cpp. The results are not compiled anyway. Tip "Undefined reference vTable for class ...", ... is the class name for these three classes. Later, it was found that when the QT program was compiled, the signal and slots definitions were invoked to convert the MOC tool into standard C + + code (signal and slots are not standard C + + keywords). The MOC tool only recognizes. h files, so be honest with the standard C + + class definition method, don't be lazy:)

http://blog.csdn.net/chaijunkun/article/details/6249573

Study on the transfer directionality of QT signal and slot mechanism (conclusion is wrong, but can look at the analysis process)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.