Results 1 to 12 of 12

Thread: [SOLVED] How can I tell qtcreator to pass parent object to promoted class?

  1. #1
    Join Date
    Feb 2010
    Posts
    17
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default [SOLVED] How can I tell qtcreator to pass parent object to promoted class?

    [edit]
    Please ignore, this has been "solved". The problem wasn't qtcreator, it was my stupidity! Although the parent isn't explicitly set at the point of instantiation, it is set when calling addWidget, which eventually makes its way to the addChildWidget method in http://qt.gitorious.org/qt/qt/blobs/...el/qlayout.cpp

    Maybe I was casting incorrectly, which may explain why it wasn't working before, but it seems fine now. Sorry for wasting your time.

    Hi,

    I'm using QtCreator to build the UI for my project. I've got a QStackedWidget object in my mainwindow and each "layer", or "page", of the stacked widget has been promoted to a custom class, derived from QWidget. Note, I've also done something similar for a wizard in my project, where each wizard page has been promoted to a custom class.

    The problem is, although the classes are derived from QWidget (and qtcreator knows this), the generated code isn't passing a parent into the constructor of the promoted class on instantiation. It's not even using the setParent method after instantiation.

    If I understand correctly (I'm a C++ newbie, feel free to correct me), it means that I will have a memory leak because I'm not deleting these objects and they don't have a parent and thus won't be deleted automatically either. It also means I can't get a reference to the parent object.

    I know I could find a pointer or reference to my objects and manually call setParent, but is there any automated way of telling qtcreator to pass a parent object on instantiation (or at least call the setParent method after instantiation on objects that are derived from QObject)?

    Thanks!
    Last edited by stevebakh; 14th March 2010 at 20:19. Reason: Solved... there was no problem, just my own stupidity.

  2. #2
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How can I tell qtcreator to pass parent object to promoted class on instantiation

    Could you show us your ui file?

  3. The following user says thank you to toutarrive for this useful post:

    stevebakh (14th March 2010)

  4. #3
    Join Date
    Feb 2010
    Posts
    17
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How can I tell qtcreator to pass parent object to promoted class on instantiation

    Hi,

    The XML content of my MainWindow ui file: http://pastebin.com/EnYmh40z

    and the specific bit of code in the generated file:

    Qt Code:
    1. stackedWidget = new QStackedWidget(centralWidget);
    2. stackedWidget->setObjectName(QString::fromUtf8("stackedWidget"));
    3. appointmentListLayer = new AppointmentListScreen();
    To copy to clipboard, switch view to plain text mode 

    I'm starting to think this may be a missing feature. QtCreator knows whether your promoted classes are derived from QObject (or QWidget in this case), so it should call setParent on the instantiated object, in my opinion. Currently, it means pointers are created in the generated file and never deleted, because they have no parent. Memory leaks.

  5. #4
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How can I tell qtcreator to pass parent object to promoted class on instantiation

    Strange, i've promoted two Qwidgets using Qt Designer (not Qt creator) and the parenting mechanism works fine.
    Try to build your UI using Designer only and see if you encounter the same problem.

  6. The following user says thank you to toutarrive for this useful post:

    stevebakh (14th March 2010)

  7. #5
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How can I tell qtcreator to pass parent object to promoted class on instantiation

    I have built the same test case using Qt creator this time and i've got no problem regarding the parenting of my promoted widgets.

  8. #6
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: How can I tell qtcreator to pass parent object to promoted class on instantiation

    Could we see one of your custom class header files?

  9. The following user says thank you to norobro for this useful post:

    stevebakh (14th March 2010)

  10. #7
    Join Date
    Feb 2010
    Posts
    17
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How can I tell qtcreator to pass parent object to promoted class on instantiation

    Thanks for testing this toutarrive.

    Did you promote the widgets to custom classes?

    I'll run another test now. The following are the exact steps I've taken to reproduce this problem:

    * Open qt-creator v1.3.1 (linux, 64bit, Qt 4.6.2)
    * Create new project: select Qt4 Gui Application, add a mainwindow default class + ui file
    * Open the mainwindow.ui file in creator and:
    * Add a QStackedWidget widget to the centralWidget.
    * Right click on the first page of the stacked widget and click "Promote to ..."
    * Make sure QWidget is selected as the BaseClass name and add classname TestPage

    -- at this stage, the code will fail to compile. we need to create the class --

    * For the project, select "Add New" and select Qt Designer Form Class
    * Select Widget from the form template options
    * Name it "TestPage", to match the name of the promoted widget above.

    -- code now compiles, check the generated code --

    After this process, when I check my generated code (ui_mainwindow.h) I see that neither the promoted class, nor the standard QWidget class have been given a parent. The code:

    Qt Code:
    1. void setupUi(QMainWindow *MainWindow)
    2. {
    3. if (MainWindow->objectName().isEmpty())
    4. MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
    5. MainWindow->resize(600, 400);
    6. centralWidget = new QWidget(MainWindow);
    7. centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
    8. stackedWidget = new QStackedWidget(centralWidget);
    9. stackedWidget->setObjectName(QString::fromUtf8("stackedWidget"));
    10. stackedWidget->setGeometry(QRect(50, 30, 421, 251));
    11.  
    12. // ***** SEE HERE *****
    13. page = new TestPage();
    14. page->setObjectName(QString::fromUtf8("page"));
    15. stackedWidget->addWidget(page);
    16.  
    17. // ***** AND HERE *****
    18. page_2 = new QWidget();
    19. page_2->setObjectName(QString::fromUtf8("page_2"));
    20. stackedWidget->addWidget(page_2);
    21.  
    22.  
    23. MainWindow->setCentralWidget(centralWidget);
    24. menuBar = new QMenuBar(MainWindow);
    25. menuBar->setObjectName(QString::fromUtf8("menuBar"));
    26. menuBar->setGeometry(QRect(0, 0, 600, 19));
    27. MainWindow->setMenuBar(menuBar);
    28. mainToolBar = new QToolBar(MainWindow);
    29. mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));
    30. MainWindow->addToolBar(Qt::TopToolBarArea, mainToolBar);
    31. statusBar = new QStatusBar(MainWindow);
    32. statusBar->setObjectName(QString::fromUtf8("statusBar"));
    33. MainWindow->setStatusBar(statusBar);
    34.  
    35. retranslateUi(MainWindow);
    36.  
    37. QMetaObject::connectSlotsByName(MainWindow);
    38. } // setupUi
    To copy to clipboard, switch view to plain text mode 

    Did you follow this procedure toutarrive, or have I done something wrong?

    Thanks again!

  11. #8
    Join Date
    Feb 2010
    Posts
    17
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How can I tell qtcreator to pass parent object to promoted class on instantiation

    Quote Originally Posted by norobro View Post
    Could we see one of your custom class header files?
    Hi norobro,

    I have just ran through a test case, shown in my previous post. I created the custom classes using QtCreator's wizard. The header looks like this:

    Qt Code:
    1. #ifndef TESTPAGE_H
    2. #define TESTPAGE_H
    3.  
    4. #include <QWidget>
    5.  
    6. namespace Ui {
    7. class TestPage;
    8. }
    9.  
    10. class TestPage : public QWidget {
    11. Q_OBJECT
    12. public:
    13. TestPage(QWidget *parent = 0);
    14. ~TestPage();
    15.  
    16. protected:
    17. void changeEvent(QEvent *e);
    18.  
    19. private:
    20. Ui::TestPage *ui;
    21. };
    22.  
    23. #endif // TESTPAGE_H
    To copy to clipboard, switch view to plain text mode 

    Thanks

  12. #9
    Join Date
    Feb 2010
    Posts
    17
    Thanks
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How can I tell qtcreator to pass parent object to promoted class on instantiation

    Hmmm, bizarre - the test case seems to work. I've just traced the code in SVN and it appears that the parent is being set. I followed it back to this file http://qt.gitorious.org/qt/qt/blobs/...el/qlayout.cpp and the addChildWidget method.

    It seems that with my test case, I can access the parent. I'm going to test again with my main project.

    [edit]
    D'oh! I don't know what's changed, but it seems to be working fine now. The parent isn't explicitly set during instantiation, but it's still set in the code mentioned in the link above. Sorry for wasting your time.
    Last edited by stevebakh; 14th March 2010 at 20:20.

  13. #10
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: How can I tell qtcreator to pass parent object to promoted class on instantiation

    Glad you solved your problem. I think toutarrive would agree that you didn't waste our time. The reason I am here is to learn and to try to help solve interesting problems like this.

    Norm

  14. The following user says thank you to norobro for this useful post:

    stevebakh (14th March 2010)

  15. #11
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How can I tell qtcreator to pass parent object to promoted class on instantiation

    You won't get any memory leak in your code even yours widgets are instatiated with no parents in their constructors.
    The deletion of the child widget of your form will be automatically handled by the Qt .

    When you use a layout, widgets (instatiated without a parent) are automatically reparented to the form 'owning' the layout (and though their deletion is automatic).
    Qt Code:
    1. stackedWidget = new QStackedWidget(centralWidget);
    2.  
    3. appointmentListLayer = new AppointmentListScreen();
    4. stackedWidget->addWidget(appointmentListLayer);
    5.  
    6. appointmentInfoLayer = new AppointmentInfoScreen();
    7. stackedWidget->addWidget(appointmentInfoLayer);
    8.  
    9. patientListLayer = new PatientListScreen();
    10. stackedWidget->addWidget(patientListLayer);
    11.  
    12. patientInfoLayer = new PatientInfoScreen();
    13. stackedWidget->addWidget(patientInfoLayer);
    14.  
    15. gridLayout->addWidget(stackedWidget, 0, 0, 1, 1); // automatic reparenting of the widgets
    To copy to clipboard, switch view to plain text mode 
    Even though you want to delete your widget manually you can do so and it will not cause an error.

    As far as i know, as your promoted widgets are part of QStackWidget (which is a class built on top of QStackedLayout) they don't need to be instatiated with a parent.
    The same applies to QTabWidget. (someone corrects me if i'm wrong).
    Last edited by toutarrive; 14th March 2010 at 21:19.

  16. The following user says thank you to toutarrive for this useful post:

    stevebakh (14th March 2010)

  17. #12
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How can I tell qtcreator to pass parent object to promoted class on instantiation

    Glad you solved your problem. I think toutarrive would agree that you didn't waste our time. The reason I am here is to learn and to try to help solve interesting problems like this.
    Could not agree more

  18. The following user says thank you to toutarrive for this useful post:

    stevebakh (14th March 2010)

Similar Threads

  1. ActiveQt: How to pass image to ActiveX object?
    By AndreasSchlempp in forum Qt Programming
    Replies: 2
    Last Post: 16th February 2009, 12:44
  2. Accessing a class Object by pointer in another class
    By pdoria in forum Qt Programming
    Replies: 2
    Last Post: 14th January 2008, 15:59
  3. Replies: 3
    Last Post: 16th May 2007, 11:07
  4. How to pass a QString to another class ?
    By probine in forum Qt Programming
    Replies: 9
    Last Post: 9th December 2006, 20:16
  5. object instantiation
    By drkbkr in forum Qt Programming
    Replies: 7
    Last Post: 29th June 2006, 02:26

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.