Results 1 to 6 of 6

Thread: Segmentation fault when trying to add GroupBox to mainLayout

  1. #1
    Join Date
    Nov 2009
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Segmentation fault when trying to add GroupBox to mainLayout

    Hi

    I get an segmentation fault when running a program I'm working on. From a button in a application based on QMainWindow, I open a new dialog to create a new activity. In the dialog I want to group the layout in group boxes.

    Qt Code:
    1. // This is the constructor of the dialog
    2. NewEvent::NewEvent(QWidget* parent) : QDialog(parent){
    3.  
    4. void createHorizontalGroupBox();
    5.  
    6. QVBoxLayout* mainLayout = new QVBoxLayout(this);
    7.  
    8. mainLayout->addWidget(horizontalGroupBox);
    9.  
    10. setLayout(mainLayout);
    11.  
    12. ...
    13. }
    14.  
    15. void NewEvent::createHorizontalGroupBox(){
    16. // horizontalGroupBox is declared in the h-file
    17. horizontalGroupBox = new QGroupBox(tr("Some text"));
    18. QHBoxLayout* layout = new QHBoxLayout;
    19.  
    20. QLabel* actLabel = new QLabel(tr("Some text"));
    21. QLineEdit* actType = new QLineEdit;
    22.  
    23. layout->addWidget(actLabel);
    24. layout->addWidget(actType);
    25.  
    26. horizontalGroupBox->setLayout(layout);
    27. }
    To copy to clipboard, switch view to plain text mode 

    What happens is that when I click the button in the application the program quits and the "segmentation fault" is returned to the terminal.

    If I remove/comment the mainLayout->addWidget(horizontalGroupBox); line, the program works the way I want.

    What could this be down to?

    Regards,

    André

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Segmentation fault when trying to add GroupBox to mainLayout

    Are you sure the code you posted compiles?
    Qt Code:
    1. NewEvent::NewEvent(QWidget* parent) : QDialog(parent){
    2.  
    3. void createHorizontalGroupBox(); //<<<--- this compiles?
    4.  
    5. QVBoxLayout* mainLayout = new QVBoxLayout(this);
    6.  
    7. mainLayout->addWidget(horizontalGroupBox);
    8.  
    9. setLayout(mainLayout);
    10.  
    11. ...
    12. }
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Nov 2009
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Segmentation fault when trying to add GroupBox to mainLayout

    Yes, it does compile.

    It's similar to the layout example in the docs (search for Basic Layouts Example).

    The function is declared as a private function in the h-file.

  4. #4
    Join Date
    Jun 2007
    Location
    Massachusetts, USA
    Posts
    24
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Segmentation fault when trying to add GroupBox to mainLayout

    Compare the example and your code again. It looks like the problem is just what high_flyer points out. Your code says:
    Qt Code:
    1. void createHorizontalGroupBox();
    To copy to clipboard, switch view to plain text mode 
    while this is more like the example:
    Qt Code:
    1. createHorizontalGroupBox();
    To copy to clipboard, switch view to plain text mode 
    Your code declares a new local method. It never defines it and never calls it. Without "void" you actually call the defined method, which creates "horizontalGroupBox", which allows
    Qt Code:
    1. mainLayout->addWidget(horizontalGroupBox);
    To copy to clipboard, switch view to plain text mode 
    to run with a good rather than NULL pointer.

  5. #5
    Join Date
    Nov 2009
    Posts
    11
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Segmentation fault when trying to add GroupBox to mainLayout

    Oh my

    I did not notice before now. It did compile, but I can see why I got the segmentation fault.

    After correction it works just fine.

    Thanks for the replies. I'm off having my eyes checked.

    André

  6. #6
    Join Date
    Nov 2009
    Posts
    1
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Segmentation fault when trying to add GroupBox to mainLayout

    I accept with code:
    void NewEvent::createHorizontalGroupBox()
    {
    // horizontalGroupBox is declared in the h-file
    horizontalGroupBox = new QGroupBox(tr("hello all"));
    QHBoxLayout* layout = new QHBoxLayout;

    QLabel* actLabel = new QLabel(tr("I love you"));
    QLineEdit* actType = new QLineEdit;

    layout->addWidget(actLabel);
    layout->addWidget(actType);

    horizontalGroupBox->setLayout(layout);
    }

Similar Threads

  1. Replies: 21
    Last Post: 28th September 2010, 11:59
  2. Replies: 2
    Last Post: 17th June 2010, 00:58
  3. Segmentation Fault when Program Starts
    By KaptainKarl in forum Newbie
    Replies: 7
    Last Post: 10th September 2009, 09:43
  4. Segmentation fault running any QT4 executables
    By jellis in forum Installation and Deployment
    Replies: 7
    Last Post: 19th May 2007, 17:35
  5. Icons missing => segmentation fault
    By antonio.r.tome in forum Qt Programming
    Replies: 4
    Last Post: 8th March 2006, 17:30

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
  •  
Qt is a trademark of The Qt Company.