简体   繁体   中英

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.

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.

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