Qt Creator error: Cannot retrieve debugging output

Qt Creator allows you to open and run multiple instances of Qt Creator at the same time. But there is a small glitch. If you are running multiple instances of Qt Creator, only the first instance displays the console output in the Application output console. The second instance would complain with the following error message:

Cannot retrieve debugging output

Fix for this is to close the second instance or open the project you want to edit and debug in the first instance.

boot2docker: Add volume from windows host to the docker container

boot2docker is great. It allows us to develop using docker even on Windows PCs. One feature that is totally awesome is its ability to share folders on windows host with boot2docker virtual box image. This shared folder from windows can then be used as a volume in the docker containers.

The first part is available to you out of the box. boot2docker shares the c:\User directory with the boot2docker virtualbox image and mounts it under /c/. Yay!

You can add this folder or part of it to the docker container as you would with normal folders.

docker run -it -v /c/:/data/ : bash

This will add the whole C:\User under /data in the launched docker container.

C: Initialize multiple variables in same declaration statement

First thing first, never initialize nor declare multiple variables on the same line. It makes things very difficult to understand. But if you have to, this post will help you.

Initializing multiple variables in same line like this:

int p, q = 10;

is ambiguous because it is hard to understand if only q is initialized to 10 or if p is also initialized to 10. Of course, in this case only q is initialized to 10. To avoid the ambiguity, each initialization can be done in its own line.

int p;
int q = 10;

Also, try to initialize automatic variables (especially pointer variables) when they are declared.

C: declaring multipe pointer variables in same declaration statement

First thing first, never declare multiple pointer variables in same declaration statement. Don’t mix declaration of pointer and non-pointer variables in same line. Following this will save you a lot of debugging time in the future.

But in case, you are forced do it or you are debugging code written by somebody else, this short post can help you.

What does this code really mean?

int* c, d;

It is clear that c is a pointer to integer. But what about d? Is it integer or pointer to integer? Ofcourse, it is integer not a pointer to integer. Using declaration as above is very ambigious. Avoid it any cost. Instead, declare them on their own statements.

int* c;
int d;

Much clear. Isn’t it?

It is also better to follow this rule when all variables are pointers. Also, I prefer to put the star next to varaible name instead of the variable type.

int *p, *q;

is more intuitive than

int* p, *q;

Dart: Getting elements to accept drops using HTML5 DnD

Using HTML5 Drag-n-Drop could be little bit tricky, because it doesn’t work out of the box. If you want to listen to on-mouse-down or on-key-down events on an element, you would do it like this:

HtmlElement element = queryselector("#my-element");
element.onMouseDown.listen((MouseEvent e) {
  print("Please don't click me hard next time! Thanks for understanding :)");
});

So, it should be that simple to get on-drop event to work right? No!
If you try this code, you will be disappointed to find out that, it doesn’t work at all.

HtmlElement element = queryselector("#my-element");
element.onMouseDown.listen((MouseEvent e) {
  print("Drop 'em on me!");
});

So why?
From the beginning of HTML, when you drop something into a web page, it is thought to be a link and the browser tries to navigate there.

So? So, you have to disable this default behavior. This is how you do it:

HtmlElement element = queryselector("#my-element");
element.onDrop.listen((MouseEvent e) {
  print("Don't drop 'em too fast, you could break them.");
});

element.onDragOver.listen((event) {
  event.preventDefault();
});

Can you notice how we prevent default behavior in onDragOver event by calling preventDefault on the event? That does the trick!

Happy Darting!!!!

Golang: Cast array/slice of a type to array/slice of interface

Suppose you have this struct:

type (
    Type struct {
        X int
    }
)

You want to convert this to an interface:

type (
    Interface interface {
    }
)

You would do that in go in the following way:

t := Type{5}
var i Interface
i = t
fmt.Println(i)

Similarly, you would expect conversion of array/slice of Type to array/slice of Interface to work:

tlist := []Type{Type{0}, Type{1}}
var ilist []Interface
ilist = tlist

That would produce the following error:

cannot use tlist (type []Type) as type []Interface in assignment

Opps. So what happened?

The reason why Golang could not convert array/slice of a type to array/slice of an interface is clearly mentioned in the official FAQ. But this fact is very easy to miss if you are new to Golang.

C++: Initializing parent class member variables in derived class constructor

Usually we initialize member variables in constructor using initialization list.

class Base {
public:
    int i;
    explicit Base(int _i) : i(_i) {
        //other initialization code
    }
};

But we cannot initialize the parent class public or protected member variable in derived class constructor’s initialization list.

class Derived: public Base {
public:
  int j;
  Derived(int _i, int _j) : i(_i), j(_j) {

  }
};

The above code will produce the following error:

par_var_init.cpp: In constructor ‘Derived::Derived(int, int)’:
par_var_init.cpp:16:28: error: class ‘Derived’ does not have any field named ‘i’
Derived(int _i, int _j) : i(_i), j(_j) {

Using parent constructor

The correct way to do this is by calling the base class constructor to initialize its variables.

class Derived: public Base {
public:
  int j;
  Derived(int _i, int _j) : Base(i), j(_j) {

  }
};

Initializing parent’s member variables in derived class constructor’s body

It is possible to initialize parent’s member variables in derived constructor’s body.

class Derived: public Base {
public:
  int j;
  Derived(int _i, int _j) : j(_j) {
    i = _i;
  }
};

Note: the following approach is wrong!

class Derived: public Base {
public:
  int i;
  int j;
  Derived(int _i, int _j) : i(_i), j(_j) {
  }
};

This will create a new variable in derived class called i that is separate from Base::i.