Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Qt signals and slots mechanism     - GAMIT learning materials finishing (Linux)

- Java Class file format parsing (Programming)

- To update Python version under CentOS system (Linux)

- CentOS 7 open ports (Linux)

- MySQL can not write the data keyword conflicts (Database)

- Oracle11g Trigger Debugging Record Error: PLS-00201: identifier SYS.DBMS_SYSTEM 'must be declared (Database)

- Linux Workstation Security Checklist - from the Linux Foundation Internal (Linux)

- How to install and configure in Ubuntu 14.10 'Weather Information Indicator' (Linux)

- Android Studio 1.0.2 set the memory size (Linux)

- Linux with Windows Explorer as a security system (Linux)

- Linux modify the network interface name (Linux)

- The three-way division of the sorting algorithm Quicksort (Programming)

- How to configure MariaDB replication in CentOS Linux (Database)

- Asynchronous communication mechanism between the Android source code analysis thread (Programming)

- Linux Command Tutorial: cat command to view the contents of the file (Linux)

- 20 Advanced Java interview questions summary (Programming)

- Use XtraBackup be physical standby database MySQL (Database)

- Linux Getting Started Tutorial: How to set up a static MAC address on VMware ESXi virtual machine (Mobile)

- Linux systems use IP masquerading anti-hacker (Linux)

- CentOS 6.6 compile and install phpMyAdmin configuration PostgreSQL9.4.1 (Database)

  Qt signals and slots mechanism
  Add Date : 2018-11-21      
  Signal groove

Signal slot in QT is a mechanism for inter-object communication, but also the core mechanism of QT. In GUI programming, we often need to change a component while another component to respond to the notification.

After the beginning of our Find button is not active, the user input're looking for, find the button is activated, this is an example of the input box and the Find button between the two components to communicate.

Early communication, between objects using a callback to achieve. Actually using the callback function pointers to implement, when something happens when we want to be able to get notification handler, it needs to be passed to the callback function pointer handler, this handler will call a callback function at the right time. Callbacks have two significant disadvantages:

They are not the type of security, we can not guarantee the handler passed to the callback function parameters are correct.
Callbacks and handlers tightly coupled, from the handler must know what is a callback function.

Signals and slots

In QT, we have a choice other than the callback technology, that is the signal slot mechanism. The so-called signals and slots are actually functions. When a specific event is triggered (as in input box character) will send a signal, and groove connection established with the signal, you can receive the signal and react (to activate the Find button).
QT components many predefined signals and slots, in GUI programming, we are accustomed to inherit those components added after inheriting our own groove, so that the way to deal with our signal. Groove and plain C ++ member function is almost the same, it can be a virtual function, can be overloaded, which can be total, private or protected, it can also be called by other member functions. It may also be a function of the parameters of any type. The only difference is: groove and a signal can also be connected together.
Unlike callbacks, signal slots mechanism is type safe. This is reflected in the groove with the function signature function signature must match the signal before passing the signal to take place. In fact, the number of parameters can be less than the number of slot parameters of the signal, because the signal channel parameter in extra parameters can be ignored. Signals and slots are loosely coupled: What kind of signals do not care class will receive its signal. Signal slots mechanism QT oh it here at the right time, channel parameters can receive signals and calls. Signals and slots can have any number of arguments, which are type-safe.

An example of a custom signals and slots

First, we need to know is that all inherit from QObject or one of its subclasses (eg QWidget) can contain signals and slots. We shall write a class inherits from QObject (or its subclasses). All contain a signal slot class must contain a macro in the upper Q_OBJECT statement.
Based on the C ++ QObject a simple class:

# Ifndef MYSTR
# Define MYSTR
#include < QObject>
#include < QString>
class MyStr: public QObject
    Macro Q_OBJECT // must be included
    MyStr () {m_value = "zero";}
    QString value () const {return m_value;}
public slots:
    void setValue (QString value);
    signals: // signal
    void valueChanged (QString newValue);
    QString m_value;
In this simple class, we can see, the use of slots to represent the slots, and the use signals to represent the signal. In fact, not so mysterious, they are macro definitions, and even public signals only macro definitions:
# Define signals public
Signal code is automatically generated by the moc, developers must not implement it in your own C ++ code.

On the contrary, the groove should be implemented by the programmer, is provided below MyStr :: setVaule () A possible

#include "MyStr.h"
void MyStr :: setValue (QString value)
    if (value! = m_value)
        m_value = value;
        emit valueChanged (value);
setValue function first comparison value and the data members of the new Senate is the same whether (later explained why do so), if not, then set the value of good data member m_value then, the signal valueChanged () sent. To whom? Class does not write, this is not the class designers are concerned, nor is it kind of concern, it just sends the signal out on the line. Then, let us set who receives this signal.

int main (int argc, char * argv [])
    MyStr a;
    MyStr b;
    QObject :: connect (& a, SIGNAL (valueChanged (QString)), & b, SLOT (setValue (QString)));
    a.setValue ( "this is A");
    return 0;
We define two class objects, a / b, use QObject :: connect () function specifies the sender signal, the receiver, tank and other information formats connect function is as follows:

 QObject :: connect (sender, SIGNAL (...), the receiver, SLOT (..));
When we call a member function setValue, in addition to the function a.m_value set to "this is A", but also to signal valueChanged () sent out is received by b.setValue, thus, the b.m_value set to " this is a ", while b.setValue again valueChanged signal is emitted, but the signal is received and no object, because we do not establish a b as the sender's any connection. At this point you should understand why the former emit need to determine value! = M_value, because without this step, and happened to set up

QObject :: connect (& b, SIGNAL (valueChanged (QString)), & a, SLOT (setValue (QString)));

Then b is a received signal, and a transmission signal is b received, so enter an infinite loop.

int main (int argc, char * argv [])
    QApplication app (argc, argv);
    MyStr a;
    MyStr b;
    QObject :: connect (& a, SIGNAL (valueChanged (QString)), & b, SLOT (setValue (QString)));
    a.setValue ( "this is A");
    QLabel * label = new QLabel;
    label-> setText (b.value ());
    label-> show ();
    return app.exec ();
We used to look at the b input label whether it has received a signal, if it is, the content should be b "this is A", output on the label, result of the program:

This example shows a way of communication between objects. Between objects can work together without having to know anything about each other. In order to achieve the purpose of communication, only need to connect them, but only through calling QObject :: connect () function to specify some simple information like.



Signal parameters should successfully connected to the tank, they must have the same parameters and the same type of order, or allow more signal than the groove, the groove will automatically ignore the extra parameters and make the call.

A signal can be connected to a plurality of slots

You can use QObject :: connect a signal to a plurality of grooves, and when the signal transmission, the order will be declared when the contact in turn calls groove.

 MyStr a;
 MyStr b;
 MyStr c;
// Signal is connected to two slots
 QObject :: connect (& a, SIGNAL (valueChanged (QString)), & b, SLOT (setValue (QString)));
 QObject :: connect (& a, SIGNAL (valueChanged (QString)), & c, SLOT (setValue (QString)));
 a.setValue ( "this is A");
// Turn calls b.setValue (), c.setValue ()
A plurality of signals can be connected with a slot

Similarly, it allows multiple signals to the same slot, and wherein each of the signal to be transmitted, that is called a groove.

 MyStr a;
 MyStr b;
 MyStr c;
// Two signals are connected to the same slot
 QObject :: connect (& a, SIGNAL (valueChanged (QString)), & c, SLOT (setValue (QString)));
 QObject :: connect (& b, SIGNAL (valueChanged (QString)), & c, SLOT (setValue (QString)));
// The following operations are calls to groove c.setValue ()
 a.setValue ( "this is A");
b.setValue ( "this is B");
A signal can be connected to another signal

Transmit a signal when the first time, they will be sent a second signal.

 MyStr a;
 MyStr b;
 MyStr c;
// Two signals are connected
 QObject :: connect (& a, SIGNAL (valueChanged (QString)), & b, SIGNAL (valueChanged (QString)));
// Re-establish the connection b and c
 QObject :: connect (& b, SIGNAL (valueChanged (QString)), & c, SLOT (setValue (QString)));
// The following operations simultaneously transmitted signal a.valueChanged and b.valueChanged
 a.setValue ( "this is A");
// So that the signal is received by the groove c.setValue b.valueChanged
The connection can be removed

// Remove the connection between b and c
  QObject :: disconnect (& b, SIGNAL (valueChanged (QString)), & c, SLOT (setValue (QString)));
In fact, when the object is delete, all its associated links will fail, will automatically remove all links QT and the object.
- Linux Services Comments (Linux)
- Large site architecture study notes (Server)
- Generic mechanism C11 standard (Programming)
- Python in yield (Programming)
- Guide: Trickle restrict application bandwidth usage (Linux)
- Python3 multi-thread download codes (Programming)
- Linux environment SSH login password instead of using the RSA Certificate (Linux)
- in no backup ex: error: Xin Tourou ah backup child process has Singles DAT / US person / bin / in no backup ex (Database)
- Use PDFBox processing PDF documents (Linux)
- Upgrading to MySQL 5.7 partitioning problem solving (Database)
- Java8 Lambda expressions and flow operations (Programming)
- systemctl Command Complete Guide (Linux)
- Five programming fallacy (Programming)
- Kali Linux virtualbox rc = Error 1908 workaround (Linux)
- How linux network security configuration (Linux)
- Thunderbird 24.4.2 has been released for use with PPA updates (Linux)
- Example of use WebSocket (Programming)
- DRBD switchover (Server)
- CentOS7 Minimal minimize installation and then install the GNOME graphical interface (Linux)
- The user how to install Notepadqq 0.41.0 under ubuntu and debian (Linux)
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.