问题
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 MainWindow
s 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