More programming tips

In the spirit of my series of programming tips articles I thought I would go ahead and throw out some more:

  • If you are using QAction, QCheckBox, QRadioButton, or similar classes, then you need to be wary of the differences between the toggled(bool) signal and the triggered(bool) (for QAction) or clicked(bool) (for button-like classes) signals.

These two signals are used in two different situations. toggled(bool) is always for if the value of the action or button being checked changes, for any reason. This means that if an QRadioButton had not been checked, and it is set to being checked, it will then emit a toggled(true). You would use this signal if you needed to update your GUI based on whether an action is checked for instance.

The other signal (triggered or clicked) are used when you need to perform an action based on a user action. For instance, if you had a ”[ ] Show Wikipedia content” on your menu bar, you may want to bring up that window as soon as the user clicks on it. On the other hand, if you were changing the checked status of the action internally to your code but didn’t want the window to pop up, you’d want to use the triggered signal.

  • If you’re not continuing to study then you’re not going to get any better at programming. I’ll generally visit (and also the C++ and Perl sub-reddits) and sometimes you’ll catch a new technique that is neat. For instance:
    • From Mr. Edd’s C++ blog: You can use the not-well-known feature of C++ to give “pointers to member data” to allow for assigning a null pointer to smart pointer classes, while not allowing any other pointers to be assigned. His writeup covers everything so I’ll refer you to that but suffice to say that something like “smart_ptr service = 0 would work, while “smart_ptr service = &newService” would not. This is useful in basically any place where you want to allow a null pointer to be assigned by not any other kind of pointer.
    • Another interesting tidbit is the restrict keyword, available in C since the C99 revision. Sadly, it is not available in C++, not even the upcoming C++0x standard, except as a compiler extension. The use of the keyword is fairly low-level, and is explained in this article for Cell processor development.

Hopefully you’ve learned something neat (and useful and not merely contrived!)