PDA

View Full Version : Custom Widget Constructor



qtoptus
11th May 2010, 15:23
I'm creating a custom widget to use from the Designer. Currently it's a simple class that has a constructor with no arguments passed. My question is how can I make this widget instantiated such that I specify a different constructor than the default, and pass arguments, for example other objects I may create problematically, not in the Designer. For instance, I have code like this in the form that uses my custom widget, generated by the Designer + VS Qt Plugin:

MyCustomWidget *myWidget1 = new MyCustomWidget();
MyCustomWidget *myWidget2 = new MyCustomWidget();

But I want something like this:

MyCustomWidget *myWidget1 = new MyCustomWidget(myObject1);
MyCustomWidget *myWidget2 = new MyCustomWidget(myObject2);

Where myObject* is created at programming time manually to encapsulate a set of callbacks for example.

Thanks.

wysota
11th May 2010, 16:05
Designer always uses the default constructor (or actually a constructor taking a QWidget parent). You should allow your widget to accept a myObject pointer through a dedicated call that can be performed after the constructor and you should make that call manually from hand written code.