PDA

View Full Version : QLabel->setText() executed in a method causes program crash



bapt
1st June 2016, 10:16
Hi all,

I have an issue : When I want to execute a QLabel->setText() in the method "modifItem", it causes a program crash.
When I execute the lcd4->setText() in the constructor, it works, but not in the method "modifItem". --> lcd4 attribute seems to not be recognized even if lcd4 lights in red... It only happens with a QLabel or a QLCDNumber... I tried with a QTableWidgetItem->setItem() and it works well.

Caisse3.cpp


Caisse3::Caisse3():QWidget()
{
QLabel*lcd4 = new QLabel();
lcd4->setFixedSize(200,30);
lcd4->setFrameStyle(QFrame::Panel | QFrame::Sunken);
QVBoxLayout *layout5 = new QVBoxLayout;
layout5->addWidget(lcd4);
}

void Caisse3::modifItem(int row, int column)
{
QString nomArticle=listBoissonsDenom[row];
lcd4->setText(nomArticle);
}


Caisse3.h


class Caisse3:public QWidget{

Q_OBJECT

public:
Caisse3();

public slots:
void modifItem(int row, int column)

private:
QLabel *lcd4;
QVBoxLayout *layout5;



Thanks for your help.

yeye_olive
1st June 2016, 11:01
In Caisse3::Caisse3(), you declare a local variable named lcd4, thus shadowing the lcd4 member of Caisse3. That member remains uninitialized (and therefore contains garbage), which causes a crash when you dereference it in Caisse3::modifItem().

bapt
1st June 2016, 11:25
Thank you !!