简体   繁体   English

创建属于Qt Designer制作的另一个小部件的Qt小部件

[英]Creating a Qt widget belonging to another widget made with Qt Designer

I have created a label in my mainwindow.cpp 我在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? 为了使它显示在我使用Qt设计器创建的布局中,我该用什么代替“ this”?

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. 首先,您应该在您在设计器中创建的小部件中显示QLabel位置添加一个QWidget 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. 对于更复杂的自定义窗口小部件很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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