Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Qt signals and slots mechanism     - Without Visual Studio .NET Windows application development (Programming)

- Close Pycharm spell check (Programming)

- Lua non-blocking write log (Programming)

- Linux / Unix: chroot command examples to explain (Linux)

- How UTorrent download the ISO image from the command line (Linux)

- How to use the Vault secure password storage and API key (Linux)

- Android Custom View step (Programming)

- Linux system monitoring, top command of the diagnostic tool Detailed (Linux)

- Retro terminal in Linux (Linux)

- Ubuntu PPA install SMPlayer 14.9 (Linux)

- Under CentOS using yum command to install the Task Scheduler crontab (Linux)

- Help you make Git Bisect (Linux)

- Linux disk and File System Concepts (Linux)

- Hadoop 2.5 Pseudo distribution installation (Server)

- Ubuntu 14.04 Docker installation (Linux)

- Swift 2.0 brief (Linux)

- Realization of Linux operating system illegal IP censorship (Linux)

- Nginx request processing (Server)

- How to configure a development environment elegant Lua (Linux)

- Recovery from MySQL master data consistency summary (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:

//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:      
 
- Help you enhance Python programming languages 27 (Programming)
- Use Ansible installation NGINX and NGINX Plus (Server)
- RHEL5 establish a local yum source (Linux)
- How to query the role of Linux services (Linux)
- File compression and packaging commands under Linux (Linux)
- Spring REST Exception Handling (Programming)
- Linux find and xargs (Linux)
- Linux Network Programming - raw socket programming (Programming)
- How to install Wine 1.7.20 under Ubuntu or Linux Mint (Linux)
- Handle large data problems Bit-map method (Programming)
- linux smartd [FAILED] appears at startup (Linux)
- Method under Linux GCC Compiler shared library function export control (Programming)
- Python dictionary applications get built-in method (Programming)
- PHP Performance Analysis and Experiment: Performance Micro Analysis (Programming)
- Use Aptik Battery Monitor monitoring Ubuntu laptop battery usage (Linux)
- Vim configuration instructions (Linux)
- Dalvik heap memory management and recycling (Linux)
- Linux Systems Getting Started Learning: Configuration PCI passthrough on a virtual machine (Linux)
- How a lot of blocking malicious IP address in Linux (Linux)
- Oracle Linux 5.5 (64bit) Install Oracle 11gR2 RAC detailed tutorial (Database)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.