QLabel does not display in QWidget

后端 未结 2 2039
醉酒成梦
醉酒成梦 2020-11-22 05:44

I have the following hierarchy in my Qt Application: QMainWindow > QWidget (centralWidget) > QWidget (subclassed) > QLabel

Initialization code in my QMainWindow code

2条回答
  •  庸人自扰
    2020-11-22 06:24

    Widgets when they are visible for the first time call to be visible to their children, but since you are creating it afterwards they probably are not calling that method, a possible solution is to call the show method.

    void ChatWidget::displayChatAfterButtonPressed()
    {
        QLabel *lbl;
        lbl=new QLabel(this);
        lbl->setText("Hello World 2");
        lbl->show();
    }
    

    comment: it seems strange to me that the QMainWindow you set a central widget and then create the chatWidget as a parent to the QMainWindow, it is generally not recommended to add children to the QMainWindow because it has a given structure, what should be done is to place it inside the centralwidget.

提交回复
热议问题