简体   繁体   English

如何使用Qt中的代码将小部件添加到中央小部件中

[英]How to add a widget into central Widget using code in Qt

I need to add a widget to the form using code, but don't by use QHBoxLayout or QVBoxLayout. 我需要使用代码向窗体添加小部件,但不必使用QHBoxLayout或QVBoxLayout。

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    fields[0] = new CustomLabel();
    fields[0]->setText("Hello");
    //how to show it  

}

You can simply use setCentralWidget() 您可以简单地使用setCentralWidget()

documentation of setCentralWidget() setCentralWidget()的文档

like this ui->setCentralWidget(fields[0]); 像这样的ui->setCentralWidget(fields[0]); or maybe with this replacing ui 或者用this替换ui

But, to displayed many widget you have to use a widget that contains the matrix of labels (widget). 但是,要显示许多小部件,您必须使用包含标签矩阵(小部件)的小部件。 When you have the parent widget that contain the matrix display it with setCentralWidget() 当您具有包含矩阵的父窗口小部件时,使用setCentralWidget()对其进行显示

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

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