Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Qt signals and slots mechanism     - Replace font under Linux (Linux)

- How to configure SNMPv3 on Ubuntu, CentOS and Cisco systems (Linux)

- HttpClient4 usage upgrade from HttpClient3 (Programming)

- Java Access Control (Programming)

- Dual system Linux (Ubuntu) into the Windows NTFS partition's mount error (Linux)

- Timing task Crontab under Linux system (Linux)

- Hadoop + Zookeeper NameNode achieve high availability (Server)

- Install Oracle JDK 8 and JVM class loading mechanism in Linux (Linux)

- To install Jetty server configuration in detail (Server)

- PLSQL often lose resolution process automatically disconnect the connection (Database)

- Ubuntu achieve initialization iptables (Linux)

- High-performance JavaScript loops and flow control (Programming)

- Common Linux System Troubleshooting (Linux)

- Using open source software to build XWiki Wiki system installed within the company (Linux)

- Nginx request processing (Server)

- Linux port scanning (Linux)

- MySQL performance view and configure finishing Daquan (Database)

- Unix system security configuration (Linux)

- MySQL optimization of the relevant Group By (Database)

- Python Django model within the class meta Detailed (Programming)

 
         
  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:

//MyStr.h
# Ifndef MYSTR
# Define MYSTR
#include < QObject>
#include < QString>
 
class MyStr: public QObject
{
    Macro Q_OBJECT // must be included
 
public:
    MyStr () {m_value = "zero";}
 
    QString value () const {return m_value;}
 
public slots:
    void setValue (QString value);
 
    signals: // signal
    void valueChanged (QString newValue);
private:
    QString m_value;
};
 
#endif
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.

detail

connection

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.
     
         
         
         
  More:      
 
- Difference in MySQL VARCHAR and CHAR data format (Database)
- Linux Security Setup Guide (Linux)
- Linux, Chrome browser font settings beautification (Linux)
- Linux desktop system using the remote server in clear text ssh password (Server)
- Linux asynchronous read documents by AIO (Programming)
- Django 1.8 TEMPLATE_DIRS configuration and STATICFILES_DIRS (Server)
- Save the database data files into Oracle Learning (Database)
- Mahout source code analysis: FP-Growth algorithm parallelization (Programming)
- Let Git variety of colors and custom log format output (Linux)
- Ubuntu Linux use ufw or iptables firewall configuration (Linux)
- Alien Magic: RPM and DEB Mutual Convert (Linux)
- CentOS 5.5 install ntop (Linux)
- SSH without password (Linux)
- SSH Filesystem use a secure connection for network file system (Linux)
- Kubernetes (k8s) environment to build combat (Server)
- How to Install terminator 0.98 on Ubuntu and Linux Mint (Linux)
- OpenSUSE GPG Comments (Linux)
- Chrome plug-in management, online-offline installation, part of the plug presentations (Linux)
- Ceph performance optimization summary (v0.94) (Server)
- Android Delete project useless resource file (Programming)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.