Evan B
Evan B

Reputation: 83

Creating a Qt widget belonging to another widget made with Qt Designer

I have created a label in my mainwindow.cpp

lblmyLabel = new QLabel(this);

What do I put in place of "this" in order to have it show up in a layout that I created with the Qt designer?

I know this is a silly question but a simple answer would open a lot of doors for me as far as understanding how this works.

Upvotes: 0

Views: 289

Answers (1)

p.i.g.
p.i.g.

Reputation: 2985

For example you can try the following.

First you shall add a QWidget where you want your QLabel to be displayed in the widget what you created in the Designer. And after that try the following lines:

void setMainWidget( QWidget* aParent, QWidget* aChild, const int aMargin = 0 )
{
    QGridLayout* layout = new QGridLayout( aParent );
    layout->addWidget( aChild );
    layout->setMargin( aMargin );
    aParent->setLayout( layout );
}

In your case:

myLabel = new QLabel( ui->widget );
setMainWidget( ui->widget, myLabel );

It is useful for more complex custom widgets.

Upvotes: 1

Related Questions