Results 1 to 6 of 6

Thread: [SOLVED] QLabel and Segmentation fault...

  1. #1
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default [SOLVED] QLabel and Segmentation fault...

    Hi,

    I have completely rebuild my project but get still a segmentation fault. I don't understand why, because the "setText()" works in the constructor but not in the function? Here is the relevant code:
    Qt Code:
    1. #include <QWidget>
    2. #include <QtGui>
    3.  
    4. class MyGraphicView : public QWidget
    5. {
    6. Q_OBJECT
    7.  
    8. public:
    9. MyGraphicView(QWidget *parent = 0);
    10.  
    11. private:
    12. void updateImageInformation();
    13.  
    14. QLabel *info_filesize;
    15. };
    16.  
    17.  
    18. //////////////////////// CPP-File ////////////////////////
    19.  
    20. #include "MyGraphicView.h"
    21.  
    22. #include <QtGui>
    23.  
    24. MyGraphicView::MyGraphicView(QWidget *parent) :
    25. QWidget(parent)
    26. {
    27. [...]
    28. info_filesize = new QLabel(this);
    29. [...]
    30. info_filesize->setText("KB"); // <- works!!!
    31. }
    32.  
    33. void MyGraphicView::updateImageInformation()
    34. {
    35. info_filesize->setText("----"); // <- SIGSEGV
    36. }
    To copy to clipboard, switch view to plain text mode 
    and GDB says:
    Qt Code:
    1. (gdb) run
    2. Starting program: /home/lykurg/workspace/cpp/workplace/ganesha/ganesha
    3. [Thread debugging using libthread_db enabled]
    4. [New Thread -1224829232 (LWP 9063)]
    5.  
    6. Program received signal SIGSEGV, Segmentation fault.
    7. [Switching to Thread -1224829232 (LWP 9063)]
    8. QLabel::setText (this=0x2f006c, text=@0xbf828400) at widgets/qlabel.h:154
    9. 154 widgets/qlabel.h: No such file or directory.
    10. in widgets/qlabel.h
    11. Current language: auto; currently c++
    To copy to clipboard, switch view to plain text mode 
    When I comment the "setText()" in updateImageInformation() out, the program starts and executes the constructor-setText without any problems.


    Thanks,

    Lykurg
    Last edited by Lykurg; 4th October 2007 at 13:03.

  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: QLabel and Segmentation fault...

    Could you show the backtrace after the crash?

  3. #3
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLabel and Segmentation fault...

    Do you have by any chance another info_filesize declared in the constructor?

  4. #4
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QLabel and Segmentation fault...

    for safety you add following which I added in blue

    MyGraphicView::MyGraphicView(QWidget *parent) :
    QWidget(parent),info_filesize(NULL)
    {
    [...]
    info_filesize = new QLabel(this);
    [...]
    info_filesize->setText("KB"); // <- works!!!
    }

    void MyGraphicView::updateImageInformation()
    {
    if(info_filesize)
    info_filesize->setText("----"); // <- SIGSEGV
    }

  5. The following user says thank you to rajesh for this useful post:

    Lykurg (4th October 2007)

  6. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QLabel and Segmentation fault...

    Hi,

    the backtrace offers nothing new. Because I am at the time not connected to the internet with my laptop and have no usb stick around, here ist the handmade bt:
    Qt Code:
    1. QLabel::setText (this=0x2f006c, text=@0xbf828400) at widgets/qlabel.h:154
    2. MyGraphicView::updateImageInformation()
    3. MyGraphicView::setImage()
    4. MyGraphicView::MyGraphicView()
    5. Ganesha:createGui()
    6. Ganesha::Ganesha()
    7. main()
    To copy to clipboard, switch view to plain text mode 

    @rajesh: Your code shows, that the label is not existing when the function is called. But why? Maybe it is because, the whole application is not shown? But the QGraphicsSceene in setImage(), which at least calls updateImageInformation(), is allready there, and the image in it is shown...

    Still stange, but I am looking forward to find the real problem.

    EDIT: Forgott about all, I am too ***! I have overlooked an updateImageInformation()-call in the constructor from previous work... damn.


    Lykurg
    Last edited by Lykurg; 4th October 2007 at 13:02.

  7. #6
    Join Date
    May 2006
    Location
    Bangalore,India
    Posts
    235
    Thanks
    7
    Thanked 25 Times in 24 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: [SOLVED] QLabel and Segmentation fault...

    may be info_filesize not getting initialize in the constructor.
    just put the breakpoint in constructor and in updateImageInformation()
    and check the address of info_filesize, if it is not a valid address then you will get Segmentation fault. may be your pointer getting disturbed some where.

Similar Threads

  1. Replies: 2
    Last Post: 19th May 2007, 18:25
  2. segmentation fault insert QString in QCombobox
    By regix in forum Qt Programming
    Replies: 16
    Last Post: 8th August 2006, 08:46

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.