繁体   English   中英

当我使用 Qt/C++ 将其他小部件添加到基本小部件时,如何增加基本小部件和所属垂直布局的高度?

[英]How can I increase the height of a basic-widget and a belonging vertical layout when I add other widgets to the basic-widget using Qt/C++?

我有一个垂直布局的“基本小部件”。 在这个布局中,我想通过单击一个按钮来添加其他小部件(这可行)。 对于每个添加的小部件,我希望“基本小部件”和布局增加添加的小部件的高度。 这最后一步不起作用。 相反,通过将更多小部件添加到“基本小部件”和布局中,添加的小部件的高度会变小。

我尝试了以下代码:

void own_widget::on_add_tbm_pb_clicked()
{
    tbm_widget *new_tbm_widget = new tbm_widget(this);
    ui->verticalLayout->addWidget(new_tbm_widget);
    this->setGeometry(this->x(), this->y(), this->width(), this->height()+100);// own_widget should grow by adding new tbm_widgets
    QRect layout_geometry(this->x(), this->y()+60, this->width(), this->height()-100);//set corresponding geometry for layout
    ui->verticalLayout->setGeometry(layout_geometry);// putting geometry onto layout

    new_tbm_widget->setGeometry(this->x(),this->y()+60+tbm_listposition*110, 400, 100);
    new_tbm_widget->show();
    
    tbm_list.append(new_tbm_widget);
    tbm_listposition ++;
}

你不应该玩几何布局。 布局将处理调整大小操作。 这就是他们的目的。 只需将小部件添加到布局中。

void own_widget::on_add_tbm_pb_clicked()
{
    tbm_widget *new_tbm_widget = new tbm_widget(this);
    ui->verticalLayout->addWidget(new_tbm_widget);
    
    tbm_list.append(new_tbm_widget);
}

如果您的布局仍未调整它们的大小,则可能是您忘记设置布局了。 或者您的尺寸政策可能是固定的。

暂无
暂无

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

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