Archive

Posts Tagged ‘Tips’

about Tools

January 16th, 2011 No comments

Tools amplify your talent. The better your tools, and the better you know how to use them, the more productive you can be.

The Pragmatic Programmer: From Journeyman to Master

Andrew Hunt (Author), David Thomas (Author)

Tags: ,

Using QString effectively

October 12th, 2010 No comments

Using QString effectively

June 22nd, 2010 No comments

QLatin1String : Avoid mallocs in operator ”==”

Creating a QString from a C-string involves a malloc. For example, there is a hidden-malloc cost in the following code.
if (fruit== "apple") { ... } // hidden malloc
In the code above, “apple” gets converted into a QString using QString::fromAscii(). This means that QString will allocate memory for the string “apple” and creates a deep copy of the C-style string. This deep copy is not really necessary and can be avoided by not creating a QString to start with and if the encoding of the C-string was somehow known. Since Latin-1 string comparisons are most common, Qt provides a special class called QLatin1String that just holds a pointer to the C-string provided in it’s constructor. In addition, QString provides a QString::operator==(const QLatin1String &) overload that has special code to compare without malloc+deep copy. We can make the above code fast by writing it instead as,
if (fruit== QLatin1String("apple")) { ... } // fast and mentions encoding

QStringRef : String manipulation without the malloc

QString has various methods for string manipulations like mid(), left(), right(). All of them create a new QString and hence a malloc/deep copy of data in an existing QString. Instead, one can use QString::midRef(), QString::leftRef() and QString::rightRef() to obtain a QStringRef. A QStringRef is a reference of a portion of a QString. QString also provides many overloads like QString::operator==(const QStringRef &) for optimizations with QStringRef.

QString::reserve and QString::squeeze

If you expect a QString to grow, it’s better to call QString::reserve to allocate extra memory in advance so that every call to QString::append() does not result in a malloc. Extra memory can be reclaimed using QString::squeeze.

QStringBuilder : Fast QString concatenation

The code below requires atleast 2 mallocs.
if (foo.startsWith("(" + type + ") 0x"))
Qt 4.6 introduces an internal class called QStringBuilder that “reserves” memory for a concatenation chain in a single shot. It does so by having each of the + operations above return a different class (not QString). This class keeps track of the string’s that are being appended and the required memory at each step. At the final step, where the concatenation operation gets converted into a QString it allocates memory in a single shot and copies all the strings in the chain one after another. This features can be enabled by using QT_USE_FAST_CONCATENATION. With this defined, one can use the operator ’%’ instead of ’+’. One would now write,
if (foo.startsWith("(" % type % ") 0x"))
If we want to use ’+’ itself instead of ’%’, one can also define QT_USE_FAST_OPERATOR_PLUS. See Fast concatenation for more details

Use QStringMatcher to match a string repetitively

If you are looking for a string repetitively in many strings or in the same string many times, you should use QStringMatcher. It uses Boyer-Moore string search algorithm to perform fast searches.

QThread

June 18th, 2010 No comments

QThread was designed and is intended to be used as an interface or a control point to an operating system thread, not as a place to put code that you want to run in a thread. We object-oriented programmers subclass because we want to extend or specialize the base class functionality. The only valid reasons I can think of for subclassing QThread is to add functionality that QThread doesn’t have, e.g. perhaps providing a pointer to memory to use as the thread’s stack, or possibly adding real-time interfaces/support. Code to download a file, or to query a database, or to do any other kind of processing should not be added to a subclass of QThread; it should be encapsulated in an object of it’s own.

You’re doing it wrong…

// create the producer and consumer and plug them together
Producer producer;
Consumer consumer;

bool bOk = producer.connect(&consumer,
                            SIGNAL(consumed()),
                            SLOT(produce()));
Q_ASSERT(bOk);
bOk = consumer.connect(&producer,
                       SIGNAL(produced(QByteArray *)),
                       SLOT(consume(QByteArray *)));
Q_ASSERT(bOk);

// they both get their own thread
QThread producerThread;
producer.moveToThread(&producerThread);

QThread consumerThread;
consumer.moveToThread(&consumerThread);

// go!
producerThread.start();
consumerThread.start();

Reference: Threading without the headache or QThread’s no longer abstract (see attached file)

8 Useful and Interesting Bash Prompts

March 28th, 2010 No comments

Many people don’t think of their command prompt as a particularly useful thing, or even pay it much attention. To me, this is a bit of a shame, as a useful prompt can change the way you use the command line. Well I’ve scoured the Interwebs looking for the best, most useful, or sometimes most amusing bash prompts. Here, in no particular order, are the ones I’d be most likely to use on my computers.

More

Tags: , , ,

Qt Creator reference card

January 21st, 2010 No comments

The QT Creator reference card is a handy reference designed to assist your use of Qt Creator. It is available in Windows/Linux as well as Mac versions and for ease of printing comes in A4 and US letter formats.

Tags: , ,

Customize the Windows 7 “Send To” Menu

January 16th, 2010 No comments

To get to the SendTo folder, you’ll need to open up an Explorer window, and then paste in the following to the address bar.

%APPDATA%\Microsoft\Windows\SendTo

%APPDATA% is an environment variable that actually maps to something like C:\users\<username>\AppData\Roaming.

Also, common “SendTo” menu is located here: C:\Users\Default\AppData\Roaming\Microsoft\Windows\SendTo

Tags: ,

Windows 7 Hotkeys

December 13th, 2009 No comments
  • Windows Key + Tab = Aero Switch (keep holding Windows key and press Tab to cycle)
  • Windows Key + E = Windows Explorer
  • Windows Key + R = Run Command
  • Windows Key + F = Search
  • Windows Key + X = Mobility Center
  • Windows Key + L = Lock Computer
  • Windows Key + U = Ease of Access
  • Windows Key + P = Projector
  • Windows Key + T = Cycle Superbar Items
  • Windows Key + S = OneNote Screen Clipping Tool (requires OneNote)
  • Windows Key + M = Minimize All Windows
  • Windows Key + D = Show/Hide Desktop
  • Windows Key + Up = Maximize Current Window
  • Windows Key + Down = Restore Down / Minimize Current Windows
  • Windows Key + Left = Dock Current Window to the Left
  • Windows Key + Right = Dock Current Windows to the Right
  • Windows Key + ‘+’ = Magnifier
  • Windows Key + Shift + Right = Moves Current Window to the Monitor on the Right
  • Windows Key + Shift + Left = Moves Current Window to the Monitor on the Left
  • Windows Key + [1-9] = Launches the corresponding program in the superbar (going left to right)
  • Windows Key + Pause = Open System Info

Additional:

77 Windows 7 Tips

October 9th, 2009 No comments

Windows 7 may be Microsoft’s most anticipated product ever. It builds on Windows Vista’s positives, and eliminates many of that OS’s negatives. It adds new functionality, too—all in a package that is less resource-hungry than its predecessor.

And whether or not you’re upgrading from Vista or skipping it altogether and moving up from Windows XP, you’ll need to know how to make the most of it in your environment. Here are 77 tips and tricks to get you there.

Visual C++ Compiler Options: /showIncludes (List Include Files)

September 17th, 2009 No comments

Causes the compiler to output a list of the include files. Nested include files are also displayed (files that are included from the files that you include).

When an include file is encountered during compilation, a message is output, for example:

Note: including file: d:\MyDir\include\stdio.h