Results 1 to 2 of 2

Thread: Own widget access causes crash

  1. #1
    Join Date
    Mar 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default 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

    ...
    Qt Code:
    1. MyWidget *w1 = new MyWidget();
    2. QString text = "text";
    3. w1->setText(text); //this always cause crash
    To copy to clipboard, switch view to plain text mode 

    mywidget.h

    Qt Code:
    1. #ifndef MYWIDGET_H
    2. #define MYWIDGET_H
    3.  
    4. #include <QWidget>
    5. #include <QLabel>
    6. #include <QGridLayout>
    7.  
    8. class MyWidget : public QWidget
    9. {
    10. Q_OBJECT
    11. public:
    12. explicit MyWidget(QWidget *parent = 0);
    13. void SetText(QString text);
    14.  
    15. signals:
    16.  
    17. public slots:
    18.  
    19. private:
    20. QLabel *label1;
    21. };
    To copy to clipboard, switch view to plain text mode 

    mywidget.cpp

    Qt Code:
    1. include "mywidget.h"
    2.  
    3. MyWidget::MyWidget(QWidget *parent) :
    4. QWidget(parent)
    5. {
    6. QGridLayout *layout = new QGridLayout();
    7. QLabel *label1 = new QLabel("Name");
    8. layout->addWidget(label1, 0, 0);
    9. setLayout(layout);
    10. }
    11.  
    12. void MyWidget::setText(QString text) {
    13. label1->setText(text);
    14. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. how can i access other form widget
    By crissti_cta in forum Newbie
    Replies: 2
    Last Post: 14th December 2010, 20:51
  2. thread GUI access crash
    By liqxpil in forum Qt Programming
    Replies: 3
    Last Post: 14th December 2010, 15:47
  3. access the widget dynamically
    By mohanakrishnan in forum Qt Programming
    Replies: 4
    Last Post: 26th November 2009, 12:37
  4. Replies: 2
    Last Post: 8th February 2008, 14:31
  5. Crash when minimizing OpenGL widget
    By MistaPain in forum Qt Programming
    Replies: 5
    Last Post: 7th October 2006, 16:58

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.