I bet my question is obvious from it's title but it may be as well not =)
So let me explain in detail... I have created a QDockWidget and then transformed (I mean promoted to..) it into a subclass using the designer.

So how do I properly access the widgets I've placed? The idea is that all the subclass' objects are dealt within the subclass. The problem (as you may be aware) is that

this->children().count()

Returns this:

QDockWidgetLayout(0x1022373d0) 
QDockWidgetTitleButton(0x102237510, name = "qt_dockwidget_floatbutton") 
QDockWidgetTitleButton(0x102235500, name = "qt_dockwidget_closebutton") 
QWidgetResizeHandler(0x102235de0) 
QAction(0x102221500) 

And none of them contain QLabels and other tish that I want to get my hands on). Looking in ui_mainwindow.h however shed some light on the matter. Widgets are on a dockWidgetContents_3 who is a QWidget that has no parent. So the only way seems to be accessing it through MainWinsow's ui->... And thinking of making ui public makes me a "very sad panda"...

Thanks in advance!

有帮助吗?

解决方案

When you say "I have created a QDockWidget and then transformed it into a subclass using the designer", the only thing I can think that this means is that you used the "promote to..." action to turn it into a placeholder for your coded subclass of a QDockWidget. What that means is it expects to place the version from your code in that spot. If your designer has widgets that are laid out within that dock widget, they will not be automatically composed into that class definition. Where they would live is as attributes on the ui object, parallel to that same dock widget.

If you wanted say a QLabel, and a QPushButton, to be composed as part of that custom subclass, then what you need to do is define them in code, and not as part of the visual layout. Then they would be members of that actual subclass.

As of right now, I believe you would find a nested QLabel right on your ui:

ui->dockWidgetContents_3
ui->pushButton_1
ui->label_1

(or however you have chosen to include your ui in your class)

Now lets say you wanted to still be able to visually design the content area of your QDockWidget. What you might do is design that as a separate widget visually. Then promote the dockWidgetContents of the QDockWidget to your custom QWidget. It will then be able to place this composition as a widget with its own children.

ui->CustomDockWidget->widget()->label_1
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top