Archive

Posts Tagged ‘Qt Tips’

Generate a Visual Studio 2010 project file from a qmake pro file

January 10th, 2011 No comments

qmake -spec win32-msvc2010 -tp vc <project_file>.pro

FPS counter for QML application

December 10th, 2010 No comments

QML_SHOW_FRAMERATE variable can be used for performance information. It prints the time spent each frame to the console.

additional info

DEFINE_BOOL_CONFIG_OPTION(frameRateDebug, QML_SHOW_FRAMERATE)

see src/declarative/util/qdeclarativeview.cpp

using:

...
if (frameRateDebug())
{
// do something
}

Also this variable can be set in Qt Creator (see the “Run Environment” subsection in the project’s “Run Settings” tab).

Tags:

QImage vs QImageReader

November 22nd, 2010 No comments

QImage is not good choice  for scaling images (like QImage::load() + QImage::scaled()).

QImageReader API is better approach than loading full image into memory and stretching it there. QImageReader API allows to set needed scaling limitations first and then load it. The image will be scaled as part of loading process and there is no needs to allocate memory for full size image.

pattern: QImageReader::setScaledSize() + QImageReader::read()

Tags:

Using QString effectively

October 12th, 2010 No comments

Saving Window Size State

August 30th, 2010 No comments
class MainWindow : public QMainWindow
{
 Q_OBJECT;
public:
 MainWindow(QWidget *parent = 0) : QMainWindow(parent)
 {
   QSettings settings;
   restoreGeometry(settings.value("mainWindowGeometry").toByteArray());
   // create docs, toolbars, etc...
   restoreState(settings.value("mainWindowState").toByteArray());
 }

 void closeEvent(QCloseEvent *event)
 {
   QSettings settings;
   settings.setValue("mainWindowGeometry", saveGeometry());
   settings.setValue("mainWindowState", saveState());
 }
};

int main(int argc, char *argv[])
{
 QApplication a(argc, argv);
 QCoreApplication::setOrganizationDomain("OrgDomain");
 QCoreApplication::setOrganizationName("OrgName");
 QCoreApplication::setApplicationName("AppName");
 QCoreApplication::setApplicationVersion("1.0.0");

 MainWindow w;
 w.show();

 return a.exec();
}

The above code will save and restore window position, size, toolbar visibility, toolbar docking area, dock states, locations and sizes. It saves using QSettings which will store your settings in a platform correct way.

Source

Qt State Machine Code Generator

August 19th, 2010 No comments

This add-in for StarUML exports modeled state machine(s) to Qt C++ code.

The add-in can speed up developing state machines when you need to only model a state machine with StarUML and then export it to source code, instead of maintaining the model and code separately.

Tags: ,

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.