简体   繁体   中英

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.

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()

documentation of setCentralWidget()

like this ui->setCentralWidget(fields[0]); or maybe with this replacing 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()

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