QLabel does not display in QWidget

主宰稳场 提交于 2019-11-26 06:08:21

问题


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

Initialization code in my QMainWindow code:

centralWidget = new QWidget();
centralWidget->setGeometry(0,0,width,height);
chatWidget=new ChatWidget(this); // the subclassed QWidget
setCentralWidget(centralWidget);

In my subclassed QWidget initialization (which happens at the same time than the Qt App initialization) I have the following code:

ChatWidget::ChatWidget(QWidget *parent):QWidget(parent)
{
    QLabel  *lbl;
    lbl=new QLabel(this);
    lbl->setText(\"Hello World 1\"); <-- Is properly Display
}

void ChatWidget::displayChatAfterButtonPressed()
{
    QLabel *lbl;
    lbl=new QLabel(this);
    lbl->setText(\"Hello World 2\"); <-- Does NOT appear
}

When the QLabel is added from the class initialization then the message is well displayed in the widget.

However when I launch the same code after a button pressed (via a function in the same QWidget subclass), then the text does not appear on screen.

I don\'t want to use layouts as I need to exactly position my labels.

Tried to repaint, but didn\'t help neither.

How can I properly and dynamically display a label after the initialization is done ?


回答1:


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.




回答2:


We need to show the label created by button click, cause centralwidget was already painted. Here is a working example, I added this as answer also I noticed better adding chatWidget as child to centralWidget where in your original code its added to the UI .. this is your choice.

Mainwindow:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    //
    ui->setupUi(this);
    centralWidget = new QWidget();
    centralWidget->setGeometry(width,height);
    chatWidget=new ChatWidget(centralWidget); // the subclassed QWidget
    setCentralWidget(centralWidget);

    // added testing
    QPushButton *btn = new QPushButton("MyButton",centralWidget);
    btn->setGeometry(100,100,100,100);
    btn->setMaximumSize(100,100);
    connect(btn,&QPushButton::clicked, chatWidget, &ChatWidget::displayChatAfterButtonPressed);
 }

and chatWidget:

ChatWidget::ChatWidget(QWidget *parent):QWidget(parent)
{
    QLabel  *lbl;
    lbl=new QLabel(this);
    lbl->setText("Hello World 1");
}

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


来源:https://stackoverflow.com/questions/48612159/qlabel-does-not-display-in-qwidget

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!