View Full Version : qglwidget and "interface" in c++

21st March 2013, 13:16
I have implemented a CViewBase class (is a sort of interface):

class CViewBase
CViewBase(void) ;
virtual ~CViewBase(void){};
virtual void GetActions(int& n){}
typedef std::shared_ptr<CViewBase*> ptrViewBase;


GetActions(int& n) set an int , but in the future must contains a custom struct with some custom data.

class CGlWidgetImage : public QGLWidget, public CViewBase
void GetActions(int & n){/*implementation*/};

now, the problem is that:
this works:

int n;
ptrGlWidgetImage ptrGlWidgetImage = std::shared_ptr<CGlWidgetImage>( new CGlWidgetImage(ptrFactory, ptrQModel, ptrModelG, ptrController, format)) ;

but this no!:

int n;
QMdiSubWindow* pSub = new QMdiSubWindow();
((CViewBase*)pSub->widget())->GetActions(n);//does nothing and not enter in //function GetActions

there is in qt a sort of tag property that can contains all type of data?
Then i can setting the data in this property.
or there is another solution?
I wish use the polymorphism because i can have many ptrGlWidgetImage each with his custom actions(commands objects)data hardcoded .
When implements a new "CGlWidgetImage" , i setting his properties in code and implements a GetActions that returns the current widget's Actions(that are show in the toolbar.)
I do this because i have an mdi application and each subwindow can contains a custom different widget, next on the subWindowActivated event i can get all actions of the current widget with a simple GetActions() and modify the toolbar.

void CMainDiag::subWindowActivated(QMdiSubWindow* pWin)
((CViewBase*)pWin->widget())->GetActions(n);//now don't works



21st March 2013, 17:33
Your whole design model will crash and burn at some point. You cannot share a single QWidget instance among multiple parents. As soon as you call setWidget() with the raw QWidget pointer (which you retrieved using get()), that transfers ownership of the widget instance from whatever the old parent was to the one for which you call setWidget(). So creating an std::shared_ptr<> will not do what you want, since as soon as the shared widget's current parent goes out of scope, it will delete the child, and you'll be left with a dangling pointer that points to a deleted object.

there is in qt a sort of tag property that can contains all type of data?

Maybe you want QVariant? You can define your own types that can be held in a QVariant, so you are not restricted to built-in and Qt types.

You seem to be very confused about your design. Why don't you explain what you want to be able to do without explaining how you think it should be done, and maybe someone here can give you some suggestions.