Own widget access causes crash
Hi, I created my own widget and when I'm trying to set something inside - Qt always crash. Do you know where is the problem?
main.cpp
...
Code:
MyWidget *w1 = new MyWidget();
w1->setText(text); //this always cause crash
mywidget.h
Code:
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
#include <QLabel>
#include <QGridLayout>
{
Q_OBJECT
public:
explicit MyWidget
(QWidget *parent
= 0);
signals:
public slots:
private:
};
mywidget.cpp
Code:
include "mywidget.h"
MyWidget
::MyWidget(QWidget *parent
) :{
layout->addWidget(label1, 0, 0);
setLayout(layout);
}
void MyWidget
::setText(QString text
) { label1->setText(text);
}
Re: Own widget access causes crash
In line #7 of your last snippet you are creating a local variable "label1" which shadows the class member variable "label1" thus the latter remains uninitialized and crashes your program when you try to dereference it.