Results 1 to 2 of 2

Thread: qglwidget and "interface" in c++

  1. #1
    Join Date
    Nov 2012
    Posts
    38
    Thanks
    3

    Default qglwidget and "interface" in c++

    hello.
    I have implemented a CViewBase class (is a sort of interface):
    Qt Code:
    1. class CViewBase
    2. {
    3. public:
    4. CViewBase(void) ;
    5. virtual ~CViewBase(void){};
    6. virtual void GetActions(int& n){}
    7. };
    8. typedef std::shared_ptr<CViewBase*> ptrViewBase;
    9.  
    10. }
    To copy to clipboard, switch view to plain text mode 
    GetActions(int& n) set an int , but in the future must contains a custom struct with some custom data.

    Qt Code:
    1. class CGlWidgetImage : public QGLWidget, public CViewBase
    2. {
    3. .
    4. .
    5. void GetActions(int & n){/*implementation*/};
    To copy to clipboard, switch view to plain text mode 
    now, the problem is that:
    this works:
    Qt Code:
    1. int n;
    2. ptrGlWidgetImage ptrGlWidgetImage = std::shared_ptr<CGlWidgetImage>( new CGlWidgetImage(ptrFactory, ptrQModel, ptrModelG, ptrController, format)) ;
    3. ((CViewBase*)ptrGlWidgetImage.get())->GetActions(n);
    To copy to clipboard, switch view to plain text mode 

    but this no!:
    Qt Code:
    1. int n;
    2. QMdiSubWindow* pSub = new QMdiSubWindow();
    3. pSub->setWidget(ptrGlWidgetImage.get());
    4. ((CViewBase*)pSub->widget())->GetActions(n);//does nothing and not enter in //function GetActions
    To copy to clipboard, switch view to plain text mode 

    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.

    Qt Code:
    1. void CMainDiag::subWindowActivated(QMdiSubWindow* pWin)
    2. {
    3. ((CViewBase*)pWin->widget())->GetActions(n);//now don't works
    4.  
    5. }
    To copy to clipboard, switch view to plain text mode 

    thanks

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: qglwidget and "interface" in c++

    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.

Similar Threads

  1. Replies: 5
    Last Post: 10th July 2010, 23:00
  2. Replies: 2
    Last Post: 22nd October 2009, 08:22
  3. Replies: 0
    Last Post: 3rd December 2008, 11:58
  4. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05
  5. QFile Problem~ "Unknow error" in "open(QIODevice::ReadWrite)"
    By fengtian.we in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2007, 15:58

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.