Segmentation fault when accessing the text of QLabel

倖福魔咒の 提交于 2019-12-13 10:24:32

问题


I got a problem with the QLabel. I got a QtWidget with a QLabel inside. Now I want to change the text of the Label with following code:

QLabel* safetyLabel = this->findChild<QLabel *>("safety_bits");

safetyLabel->setText(QString("test"));
printf("%i", (safetyLabel->text()).length());

but I always get a "Segmentation fault". I think it's something quite simple, but I just can't see it...

Any ideas?


回答1:


Your safetyLabel can be NULL if you use QtCreators' designer to build your UI and execute your code before you call ui->setupUi(this); in MainWindows constructor.




回答2:


Here is the code.

QLabel *safetyLabel = NULL;
safetyLabel = (QLabel *) this->findChild("safety_bits");
if(!safetyLabel)
{
    qDebug() << "Failed to find safety_bits label!";
    return 1;
}
safetyLabel->setText(QString("safety_bits is here"));


来源:https://stackoverflow.com/questions/14909809/segmentation-fault-when-accessing-the-text-of-qlabel

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