1+1=10

Debao's Blog...

"How to use QThread in the right way (Part 1)"

A short history

Long long ago, subclass QThread and reimplement its run() function is the only recommended way of using QThread. This is rather intuitive and easy to used. But when SLOTS and Qt event loop are used in the worker thread, some users do it wrong. So Bradley T ...

"left shift operator overloading for QDebug()"

Consider that we have create a custom type:

struct Point
{
Point(int x, int y):x(x),y(y){}
int x;
int y;
};

If we want to make it work with qDebug(), we need to implement a streaming operator:

QDebug operator<<(QDebug dbg, const Point &p)

But, wait ...

When we ...

"Link Confilict between SDL and Qt under Windows"

Someone complain that, when using SDL and Qt in the same project under Windows, the linker will generate link error.

SDLmain.lib(SDL_win32_main.obj):-1: error: LNK2005: _WinMain@16 already defined in qtmaind.lib(qtmain_win.obj)

What happened behined this?

#include <QApplication>
#include <SDL.h>
int main(int argc, char ...

"Redirect current process's stdout to a Widget such as QTextEdit"

Note:

  • Source code can be got from https://github.com/dbzhang800/StdoutRedirector
  • This class can only be used in Qt5, as QWindowsPipeReader which is introduced in Qt5.0 is used.

Implementation

Windows

  • Normally, we need to create pipe with CreatePipe(), then attach stdout to it's write end with SetStdHandle ...

"Qt Macro: Q_DECLARE_INTERFACE"

Q_DECLARE_INTERFACE(InterfaceClassName, InterfaceId)

This macro associate the given InterfaceId to the interface class called InterfaceClassName.The macro is normally used right after the interface definition:

class MyInterface {};
Q_DECLARE_INTERFACE(MyInterface, "me.debao.qt.myinterface")

Q_DECLARE_INTERFACE is a macro that defines helper function that make qobject_cast(QObject *object) return a IFace pointer ...

"Qt Macro: Q_DECLARE_METATYPE"

QMetaType

  • It associates a type name to a type ID, enabling construction and destruction to occur dynamically at runtime.
  • QMetaType is used as a helper in QVariant and queued signals and slots connections.

```cpp example1

include

class MyClass { public: MyClass() {qDebug("Created");} ~MyClass() {qDebug("Destroyed");} };

int main(int argc, char ...

"Qt Macro: Q_DECLARE_TYPEINFO"

Definition

Q_DECLARE_TYPEINFO is used to specialise a template class called QTypeInfo.

```cpp qtypeinfo.h

define Q_DECLARE_TYPEINFO_BODY(TYPE, FLAGS) \

class QTypeInfo \ { \ public: \ enum { \ isComplex = (((FLAGS) & Q_PRIMITIVE_TYPE) == 0), \ isStatic = (((FLAGS) & (Q_MOVABLE_TYPE | Q_PRIMITIVE_TYPE)) == 0), \ isLarge = (sizeof(TYPE)>sizeof(void)), \ isPointer = false, \ isDummy = (((FLAGS) & Q_DUMMY_TYPE) != 0), \ sizeOf = sizeof(TYPE) \ }; \ static inline const char name ...

"screen.css used by octopress"

screen.css

The css file used by octopress blog with the default theme is called screen.css, which locates in source/stylesheets/screen.css.

screen.scss

The screen.scss file, which locates in sass/screen.scss, is used to generate the above css file.

```scss screen.scss @import "compass"; @include ...

"My first C Extension To Numpy"

A Simple Example

Let's create an extension module called opee and let's say we want to create a Python interface to one C function example. This function takes a numpy array as argument and return an integer. We want this function to be callable from Python as follows ...

"Sandbox: For Octopress Usage and Markdown Syntax Test"

Table Test

Source Code:

Left Aligned Header | Center Header | Right Aligned Header
:-------------------|:-------------:|--------------------:
Cell 0 0            | Cell 0 1      | Cell 0 2
Cell 1 0            | Cell 1 1      | Cell 1 2
 |  | 
Long Long Long Long Line  | Not so long Line  | Short Line

Preview:

Left Aligned Header Center Header Right Aligned Header ...