[Insight-users] Returning a value of the same same type as a class template

Arnaud GELAS arnaud_gelas at hms.harvard.edu
Tue Feb 8 09:24:04 EST 2011


David,

What about making Form a template class where you define all you need?

Generally it is not recommended to do so with Qt (at the moc' time). But 
you could create a template class (e.g. FormBase) which inherit from 
QObject or QWidget (?), where you define all types, implements all 
needed methods *but without* any signals / slots (*without* Q_OBJECT)...
Then you can inherit from this class and specialize. In this inherited 
class you can now provide signals / slots (which wrap methods from the 
parent class).

HTH
Arnaud

On 02/08/2011 08:19 AM, David Doria wrote:
> I frequently have this situation:
>
> - A Qt form object called Form.
> - I want to store an object of a class which encapsulates a particular 
> functionality, say ImageSegmentation in the Form object
> - ImageSegmentation is templated on the type of image it should 
> operate on, and this type is specified by a user action at runtime
> - Because of this, I can't store a Segmentation<T> as a member 
> variable in Form because I don't know T.
> - I've been overcoming this by creating a non-templated 
> SegmentationBase class that I can store a pointer to in Form (i.e. 
> SegmentationBase* MySegmentationBase;
> - I then instantiate the class at runtime with
> MySegmentationBase = new Segmentation<itk::Image<unsigned char, 2> >();
>
> This has been working, except that I end up jumping through hoops to 
> get the output of the segmentation (or whatever operation I am doing 
> with this pattern). That is, I want to have a
>
> template <typename T>
> class Segmentation : public SegmentationBase
> {
>  T GetResult();
> }
>
> that I can call from:
>
> Form::SomeFunction()
> {
>  T result = MyImageSegmentationBase->GetResult();
>  ... do something with 'result' ...
> }
>
> The problem with that is two fold.
>
> 1) GetResult is only defined in Segmentation (not the Base) because 
> the return type is T.
> 2) I don't know T in SomeFunction(), so I can't get the value anyway.
>
> I tried to fix (1) by making a pure virtual GetImage() in 
> SegmentationBase which returns a itk::DataObject::Pointer and also 
> making Segmentation return a itk::DataObject::Pointer, but it has 
> trouble with the conversion from T::Pointer 
> to itk::DataObject::Pointer. But even if that worked, I'd be stuck 
> with (2).
>
> Here is the whole layout:
> http://codepad.org/lWvNYc2g
>
> Any suggestions? I hope I am just making this more complicated than it 
> needs to be, because it seems like a pretty standard thing to want to do!
>
> Thanks,
>
> David

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20110208/0c45607d/attachment.htm>


More information about the Insight-users mailing list