Results 1 to 3 of 3

Thread: QLabel->setText() executed in a method causes program crash

  1. #1
    Join Date
    Jun 2016
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default QLabel->setText() executed in a method causes program crash

    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
    Qt Code:
    1. Caisse3::Caisse3():QWidget()
    2. {
    3. QLabel*lcd4 = new QLabel();
    4. lcd4->setFixedSize(200,30);
    5. lcd4->setFrameStyle(QFrame::Panel | QFrame::Sunken);
    6. QVBoxLayout *layout5 = new QVBoxLayout;
    7. layout5->addWidget(lcd4);
    8. }
    9.  
    10. void Caisse3::modifItem(int row, int column)
    11. {
    12. QString nomArticle=listBoissonsDenom[row];
    13. lcd4->setText(nomArticle);
    14. }
    To copy to clipboard, switch view to plain text mode 

    Caisse3.h
    Qt Code:
    1. class Caisse3:public QWidget{
    2.  
    3. Q_OBJECT
    4.  
    5. public:
    6. Caisse3();
    7.  
    8. public slots:
    9. void modifItem(int row, int column)
    10.  
    11. private:
    12. QLabel *lcd4;
    13. QVBoxLayout *layout5;
    To copy to clipboard, switch view to plain text mode 

    Thanks for your help.
    Last edited by anda_skoa; 1st June 2016 at 11:55.

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QLabel->setText() executed in a method causes program crash

    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().

  3. #3
    Join Date
    Jun 2016
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QLabel->setText() executed in a method causes program crash

    Thank you !!

Similar Threads

  1. QLabel ui->name_pc->setText(nam);
    By Zoltan in forum Newbie
    Replies: 1
    Last Post: 14th July 2012, 01:00
  2. QLabel->setText() sometimes not working
    By cass in forum Newbie
    Replies: 8
    Last Post: 16th May 2010, 13:12
  3. QLabel setText Problem
    By pmabie in forum Qt Programming
    Replies: 10
    Last Post: 1st November 2007, 23:32
  4. Problem with QLabel and setText
    By jambrek in forum Qt Programming
    Replies: 7
    Last Post: 31st October 2007, 16:02
  5. QLabel::setText() how to
    By freak in forum Qt Programming
    Replies: 1
    Last Post: 30th October 2006, 17:19

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.