简体   繁体   中英

Qt designer: nested widget is not a member?

Im using Qt Creator 3.2.1. Using the Qt Designer I made the basic design: I added a custom widget ( CustomWidget widget ) to my mainwindow by adding a normal QWidget and promoting it to my custom one. Within that custom widget I added a QGraphicsView ( QGraphicsView view ).

The Object browser of Qt Designer displays the hierarchy of the items correctly: view within widget and widget within mainwindow .

However, the ui_mainwindow.h that is generated by the form gives me:

// in ui_mainwindow.h
class Ui_MainWindow
{
public:
    CustomWidget *widget;
    QGraphicsView *view;
    // ...
};

When I really expected:

// in ui_mainwindow.h
class Ui_MainWindow
{
public:
    CustomWidget *widget;
    // ...
};

// in customwidget.h
class CustomWidget
{
public:
    QGraphicsView *view;
    // ...
};

Is a nested widget of another widget not supposed to be a member of it?

How to adjust my class design in the Qt Designer to make view member of widget ?

In Qt , the rule which is used in defining widget hierarchies is not containment but parenting. widget1 of type WidgetType1 will be drawn inside widget2 of type WidgetType2 if widget2 is the parent of widget1 , not if the class WidgetType2 contains a member of type WidgetType2 . widget2 can be set parent of widget1 by passing widget2 as parent in the constructor of widget2 or by using widget2.setParent(widget1); . Layouts also can define Parent/child hierarchies.

Yes, you can have types of objects which contain and define other objects, but if you don't pass this as parent to the member objects, the member objects won't be drawn inside the container class type widgets.

In your case there is no need to have multiple classes, you just have to make the objects you want to be containers parents to objects you want to be children. For more info I recommend reading the Qt documentation for Layout Management and Object Trees & Ownership .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM