开发者

difference between connections in qt

开发者 https://www.devze.com 2023-01-07 18:21 出处:网络
I have some constructor for the class LCDRange: LCDRange::LCDRange(QWidget *parent) : QWidget(parent) {

I have some constructor for the class LCDRange:

 LCDRange::LCDRange(QWidget *parent)
     : QWidget(parent)
 {
     QLCDNumber *lcd = new QLCDNumber(2);
     lcd->setSegmentStyle(QLCDNumber::Filled);

     slider = new QSlider(Qt::Horizontal);
     slider->setRange(0, 99);
     slider->setValue(0);

     connect(slider, SIGNAL(开发者_开发百科valueChanged(int)),
             lcd, SLOT(display(int)));
     connect(slider, SIGNAL(valueChanged(int)),
             this, SIGNAL(valueChanged(int)));

     QVBoxLayout *layout = new QVBoxLayout;
     layout->addWidget(lcd);
     layout->addWidget(slider);
     setLayout(layout);
 }

but I can't understand the difference between these lines:

connect(slider, SIGNAL(valueChanged(int)),
                 lcd, SLOT(display(int)));
connect(slider, SIGNAL(valueChanged(int)),
                 this, SIGNAL(valueChanged(int)));

thanks in advance


Remember that we are in OO land, so we are operating on objects.

In order to connect a signal to something, those signal/slots have to be apart of an object.

Another way to think about it is you can't have a signal by itself, because a signal has to come from somewhere. Likewise, you can't have a slot all by itself it has to be attached to something.

Now, Qt allows you to connect a signal to a slot, but it also allows you to connect a signal to a signal, which is what is happening in the second connect line. This allows you to chain signals from one object to another, or you could think about it as passing a signal through.

Usually this happens when an Object or Widget has a private internal child that emits a signal, and the parent Object/Widget wants to emit the same signal to whoever is listening to it.

In the case of your example the LCDRange Widget contains a slider, which is an internal implementation detail. The user of LCDRange doesn't need to know about all the different components that make up the LCDRange (slider, lcdnumber, layout, etc), that's a basic requirement of encapsulation which is a huge advantage of OO.

However, the user of LCDRange will want to know when the value of LCDRange changes. And rather then creating a slot in LCDRange, that simply re-emits the signal valueChanged signal, you can forward or passthrough the valueChanged signal from the slider.

The following achieves the same effect of passing the signal through, however it requires much more code.

class LCDRange : public QWidget
{
// constructor and other methods
//...
signals:
    void valueChanged(int)
private slots:
    void sliderValueChanged(int);
// ...
};

// in the LCDRange constructor
connect(slider, SIGNAL(valueChanged(int)),
                 this, SLOT(sliderValueChanged(int)));

// elsewhere in LCDRange's definition

void LCDRange::sliderValueChanged( int value )
{
    emit valueChanged( value );
}


connect(slider, SIGNAL(valueChanged(int)),
                 lcd, SLOT(display(int)));

tells, if the value of the slider gets changed then valueChanged signal will be emited and it will be reflected in LCD view. i.e display slots gets called, whose implementation will be present in QLCDNumber

connect(slider, SIGNAL(valueChanged(int)),
                 this, SIGNAL(valueChanged(int)));

Qt offers, mechanism of connecting one signal to another signal, it implies if slider valueChangedsignal is emited means present class valueChangedsignal is emited. the connected slot for valueChanged gets called

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号