QT written in the constructor control does not display (button is not displayed) problem

First, the problem:

There you will find the time to run a blank window was found, nothing is displayed, what causes it after you write buttons and other controls in the new project inside the constructor to initialize?

Second, the reason may occur:

1, your new project engineer MainWindow subclass, the parent window is not set.

2, did not set the control's parent window widget define yourself.

eg:

#include<QMainWindow>

QMainWindow::QMainWindow(QMainWindow*parent) :
    QMainWindow(parent),
    ui(new Ui::QMainWindow)
{
     ui->setupUi(this);
     QPushButton* button_1 = new QPushButton("add");
     QPushButton* button_1 = new QPushButton("del");
}

Third, the results:

A blank form

Fourth, the solution:

Solution 1:

Button control to set the parent window: QWidget, and add a button to the parent window.

#include<QMainWindow>
#include<QPushButton>
#include<QHBoxLayout>

QMainWindow::QMainWindow(QMainWindow*parent) :
    QMainWindow(parent),
    ui(new Ui::QMainWindow)
{
     ui->setupUi(this);
     QWidget* w = new QWidget();
     this->setCentralWidget(w);
     QHBoxLayout* hLayout = new QHBoxLayout();
     QPushButton* button_1 = new QPushButton("add");
     QPushButton* button_1 = new QPushButton("del");
     hLayout->addWidget(button_1);
     hLayout->addWidget(button_2);
     w->setLayout(hLayout);
}

Solution 2:

Built directly QWidget project

#include<QWidget>
#include<QPushButton>
#include<QVBoxLayout>

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

        QPushButton* button_1 = new QPushButton("add");
        QPushButton* button_2 = new QPushButton("del");
        QHBoxLayout* hLayout = new QHBoxLayout();
        QVBoxLayout* vLayout = new QVBoxLayout();

        hLayout->addWidget(button_1);
        hLayout->addWidget(button_2);
        this->setLayout(hLayout);
}

Solution 3:

#include<QMainWindow>
#include<QPushButton>
#include<QHBoxLayout>

QMainWindow::QMainWindow(QMainWindow*parent) :
    QMainWindow(parent),
    ui(new Ui::QMainWindow)
{
     ui->setupUi(this);
    
    
     QPushButton* button_1 = new QPushButton("add");
     QPushButton* button_1 = new QPushButton("del");
     button_1->setParent(this);
     button_2->setParent(this);
     button_2->move(300,100);
    
}

V. Conclusion:

Want a form of control is visible, you must set the form to the parent window for these controls.

How to make the new project parent-child relationship:

(1) to add controls to the layout manager, QT will automatically generate parent-child relationship. Such solutions 1,2, a button can be added directly to the layout manager, do not need to QWdiget, added here for clear, you may not be required.

(2) If the new QMainWindow, needs its own designated control's parent form. For example Solution 3

 

 

 

Published 23 original articles · won praise 4 · Views 9964

Guess you like

Origin blog.csdn.net/hxp1994/article/details/103882980