PDA

View Full Version : Some questions related to Qt tutorials



jamadagni
17th March 2007, 07:21
I have some questions I collected when I was offline and doing the Qt tutorials. Please read patiently and answer. Thank you very much.

1. Is there any effective practical difference between presence and absence of setFocusProxy in lcdrange.cpp of tutorial 08? I tried doing without it and did not see any change.

2. In tut 08 it says:

"It is important to only emit the angleChanged() signal when the angle really has changed."
Why? is it just good programming practice leading to efficiency or what?

3. In tut 09 line 56 we have:

painter.translate(0, rect().height());
But in tut 10 line 68:

painter.translate(0, height());
The rect() is unnecessary, right? Shouldn't it cause any inefficiency? I found that after compiling there is no difference even in the binaries!

4. In tut 10 we have:

include <QPaintEvent>
whereas in 09 we do not. Is there not a difference?

5. What are the historical reasons mentioned in the QRect class reference of bottomLeft() and bottomRight() ?

6. In tut 10 what is the effective difference between update() and update(cannonRect())? I see no problem with just update(). Then the cannonRect function is unnecessary.

7. In tut 08 cannonfield.cpp line 49 why is "event" within /**/ ?

8. In tut 08, I used:

connect ( angle, SIGNAL ( valueChange ( int ) ), cannonField, SLOT ( setAngle ( int ) ) ) ;
instead of

connect ( angle, SIGNAL ( valueChanged ( int ) ), cannonField, SLOT ( setAngle ( int ) ) ) ;
but got no compile-time error saying that there is no such signal called "valueChange ( int )". How come?

Thanks for your patience.

wysota
17th March 2007, 09:17
I'll answer those that I can without looking at the tutorial :)

2. In tut 08 it says:

Why? is it just good programming practice leading to efficiency or what?
Yes, it's mostly a matter of efficiency, but not only. If the signal names suggests that something changed, then it should only be emitted when something changed and not when it might have changed. Consider this small example:

QSpinBox *sb = new QSpinBox;
QSlider *sl = new QSlider;
connect(sb, SIGNAL(valueChanged(int)), sl, SLOT(setValue(int)));
connect(sl, SIGNAL(valueChanged(int)), sb, SLOT(setValue(int)));
If valueChanged() was emitted every time setValue() was used - even if the new value was exactly the same as the old one, you'd end up with an infinite loop here. Making sure that the signal is emitted only when values change, the code will work correctly.


3. In tut 09 line 56 we have:

painter.translate(0, rect().height());
But in tut 10 line 68:

painter.translate(0, height());
The rect() is unnecessary, right? Shouldn't it cause any inefficiency? I found that after compiling there is no difference even in the binaries!
Yes, it should behave the same.


4. In tut 10 we have:

include <QPaintEvent>
whereas in 09 we do not. Is there not a difference?
No, because some other class might already include QPaintEvent.


6. In tut 10 what is the effective difference between update() and update(cannonRect())? I see no problem with just update(). Then the cannonRect function is unnecessary.
The former refreshes the whole widget whereas the latter only the part where the cannon is. It might make it faster.


7. In tut 08 cannonfield.cpp line 49 why is "event" within /**/ ?
Because the value of event is never used in the method and leaving the variable name would cause a warning about an unused variable.


8. In tut 08, I used:

connect ( angle, SIGNAL ( valueChange ( int ) ), cannonField, SLOT ( setAngle ( int ) ) ) ;
instead of

connect ( angle, SIGNAL ( valueChanged ( int ) ), cannonField, SLOT ( setAngle ( int ) ) ) ;
but got no compile-time error saying that there is no such signal called "valueChange ( int )". How come?
Because slots are connected during runtime. The construction above is completely valid from compiler's point of view. You should get a runtime warning about a missing signal.

danadam
17th March 2007, 10:51
1. Is there any effective practical difference between presence and absence of setFocusProxy in lcdrange.cpp of tutorial 08? I tried doing without it and did not see any change.
LCDRange consists of two widgets: QLCDNumber and QSlider. In main.cpp in MyWidget constructor you create LCDRange and call setFocus() on it. With setFocusProxy() focus is passed to QSlider contained in it. So in this example, the practical difference is right after you run the program. With setFocusProxy() slider has focus. Without setFocusProxy() you have to press Tab first.