简体   繁体   English

我想使用 Qt 框架在新小部件中添加标签

[英]I want to add a label in new widget using the Qt framework

This is my code :这是我的代码:

void maquette::on_btn_edit_clicked()
{
    QWidget* wdg = new QWidget;
    wdg->resize(320, 340);
    wdg->setWindowTitle("Modiffier");
    QLabel label1("matricule", wdg);
    label1.setGeometry(100, 100, 100, 100);
    wdg->show();
}

the window shows up but the label didn't show窗口出现但标签没有显示

void maquette::on_btn_edit_clicked()
{
 QWidget *wdg = new QWidget;
 wdg->resize(320,340);
 wdg->setWindowTitle("Modiffier");
 QLabel *label1 = new QLabel("matricule",wdg);
 label1->setGeometry(100, 100, 100, 100);
 wdg->show();
}

You can either add the QLabel using parenting.您可以使用父级添加 QLabel。 as mentioned before.正如先前所说。

QLabel *label1 = new QLabel("matricule",wdg);

or或者

QLabel *label1 = new QLabel("matricule");
label1->setParent(wdg);

This will make the widget float inside its parent.这将使小部件浮动在其父级内。

You can also add the QLabel to a layout that has been assigned to the QWidget.您还可以将 QLabel 添加到已分配给 QWidget 的布局中。

QVBoxLayout* layout = new QVBoxLayout();
wdg->setLayout(layout);
QLabel *label1 = new QLabel("matricule");
layout->addWidget(label1);

This will add the widget to the layout.这会将小部件添加到布局中。 The layout will control how the child widgets are laid out.布局将控制子小部件的布局方式。

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

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