1
votes

I have two examples where I'm basically creating wrapper objects rather than what would ideally be a simple conversion.

If foo is a QWidget* instantiated earlier, can I avoid creating a wrapper QLayout for it:

const auto layout = new QVBoxLayout();
layout->addWidget( foo );
const auto frame = new QLabel( QLatin1String( "Why Do I Need a Layout?" ) );
frame->setLayout( layout );

If foo is a QLayout* instantiated earlier, can I avoid creating a wrapper QWidget for it:

const auto widget = new QWidget();
widget->setLayout( foo );
const auto tabs = new QTabWidget();
tabs->addTab( widget, QLatin1String( "Why Do I Need a Widget?" ) );
2
I'm not sure what you're asking, or what you want do achieve. Could you maybe add some details? - MatthiasB
layout is a wrapper, I don't need the QLayout, it doesn't do anything, but I can't call frame->setWidget( foo ); cause QLabel doesn't have that function. I'm asking if there's a way I can skip the layout wrapper. Same thing for the widget wrapper. - Jonathan Mee
Just to clarify: You want to add a child to a Widget without adding a Layout, and you want to add a Layout as child where a QWidget is expected? - MatthiasB
Widget doesn't have to have a layout. You can add child widgets by just setting it as their parent, but then you will have to adjust the size and position of the child widgets manually. - thuga
@JonathanMee Yes, it's regarding the first case. I'm not saying it will achieve the same behavior. Like I said you will have to adjust the child widget's size and position manually. - thuga

2 Answers

2
votes

Just can always write your own wrapper functions:

QLayout* wrap(QWidget* w){
    auto layout = new QVBoxLayout();
    layout->addWidget( w );
    return layout;
}
QWidget* wrap(QLayout* l){
    auto widget = new QWidget();
    widget->setLayout( l );
    return widget;
}
1
votes

Widget doesn't have to have a layout. You can add child widgets to a widget by setting the widget as their parent. But then you will have to adjust the size and position of the child widgets manually.