Results 1 to 3 of 3

Thread: Simple problem with layouts and maybe QGroupBox. Segmentation Fault

  1. #1
    Join Date
    May 2007
    Location
    Warsaw, Poland
    Posts
    52
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Simple problem with layouts and maybe QGroupBox. Segmentation Fault

    At first: Hi all, that's my first message here and I hope not the last one.

    I have been writing quick weekend code for relaxation and I have encountered quite an odd problem. It is related to layouts. Below I have placed the code. It's not a long one so I encourage you to compile it and check.

    It is a nuisance for me as, frankly speaking, I don't understant why it doesn't work properly.

    When you delete /* */ (comment signs) in arranging method there happens a SEGMENTATION FAULT. Conversely, if you leave them, everything is fine.

    I was hesitating where to place this post but finally I decided to put it in Qt Programming instead of Newbie.

    File renamer.h
    Qt Code:
    1. #ifndef RENAMER_CLASS_DEFINITION_SAFETY_DECLARATION
    2. #define RENAMER_CLASS_DEFINITION_SAFETY_DECLARATION
    3. //deklaracja klasy RenamerWidget
    4. #include <QWidget>
    5. #include <QObject>
    6.  
    7. class QLabel;
    8. class QGroupBox;
    9. class QLineEdit;
    10. class QSpinBox;
    11. class QRegExp;
    12. class QValidator;
    13.  
    14. class RenamerWidget : public QWidget {
    15. Q_OBJECT
    16. public:
    17. RenamerWidget(QWidget *parent = 0);
    18. private slots:
    19. void initialization(void);
    20. void arranging(void);
    21. void selectFiles(void); // Button "Choose Files"
    22. void alterNames(void); // Button "Zmien nazwy"
    23. void checkInput(void); // decyduje czy przycisk "Zmien nazwy" jest dostepny
    24. void constDigitsSpin(bool); // decyduje czy spin przy radio buttonie jest dostepny
    25. void resetSpinRange(bool); // zmienia na zakres domyslny spina cyfr poczatkowej
    26. void changeSpinRange(int); // zmienia zakres spina zaleznie od wybranej wartosci w 1 spinie
    27. private:
    28. static const int MAX_START_NUMBER = 999;
    29. QPushButton *chooseFiles_Button;
    30. QPushButton *exit_Button;
    31. QPushButton *changeNames_Button;
    32. QLabel *howManyFiles_L;
    33. QGroupBox *newName_GB;
    34. QGroupBox *numerationStyle_GB;
    35. QLabel *lineEditDescription_L;
    36. QLineEdit *chain_LE;
    37. QRadioButton *constantAmountOfDigits_RB;
    38. QRadioButton *elasticAmountOfDigits_RB;
    39. QButtonGroup *radioGroup_BG;
    40. QSpinBox *howManyDigits_SB;
    41. QLabel *startingDigit_L;
    42. QSpinBox *startingDigit_SB;
    43. QRegExp *rx;
    44. QValidator *validator;
    45. };
    46. #endif
    To copy to clipboard, switch view to plain text mode 

    File renamer.cpp
    Qt Code:
    1. //definicja klasy renamer
    2. #include "renamer.h"
    3. #include <QPushButton>
    4. #include <QApplication>
    5. #include <QLabel>
    6. #include <QRadioButton>
    7. #include <QGroupBox>
    8. #include <QLineEdit>
    9. #include <QButtonGroup>
    10. #include <QSpinBox>
    11. #include <QRegExp>
    12. #include <QValidator>
    13. #include <QGridLayout>
    14.  
    15. #include <QVBoxLayout>
    16. #include <QHBoxLayout>
    17.  
    18. RenamerWidget::RenamerWidget(QWidget *parent) : QWidget(parent) {
    19. setWindowTitle(tr("MTRenaMeR"));
    20. setFixedSize(500,300);
    21. initialization();
    22. qWarning("initialization();");
    23. arranging();
    24. qWarning("arranging();");
    25. show();
    26.  
    27. }
    28.  
    29. void RenamerWidget::initialization(void) {
    30. chooseFiles_Button = new QPushButton(tr("Wybierz pliki"));
    31. exit_Button = new QPushButton(tr("Wyjdz"));
    32. changeNames_Button = new QPushButton(tr("Zmien nazwy"));
    33. changeNames_Button->setEnabled(false);
    34. connect(exit_Button, SIGNAL(clicked()), qApp, SLOT(quit()));
    35. connect(changeNames_Button, SIGNAL(clicked()), this, SLOT(alterNames()));
    36. connect(chooseFiles_Button, SIGNAL(clicked()), this, SLOT(selectFiles()));
    37. howManyFiles_L = new QLabel(tr("Nie wybrano zadnych plikow."));
    38. chain_LE = new QLineEdit;
    39. rx = new QRegExp("[A-Za-z0-9]{1,25}_");
    40. validator = new QRegExpValidator(*rx,0);
    41. chain_LE->setValidator(validator);
    42. connect(chain_LE, SIGNAL(textChanged(const QString &)), this, SLOT(checkInput()));
    43.  
    44. constantAmountOfDigits_RB = new QRadioButton(tr("Stala ilosc cyfr"));
    45. elasticAmountOfDigits_RB = new QRadioButton(tr("Zmienna ilosc cyfr"));
    46. radioGroup_BG = new QButtonGroup;
    47. radioGroup_BG->addButton(constantAmountOfDigits_RB,1);
    48. radioGroup_BG->addButton(elasticAmountOfDigits_RB,2);
    49. connect(constantAmountOfDigits_RB, SIGNAL(clicked(bool)), this, SLOT(constDigitsSpin(bool)));
    50. howManyDigits_SB = new QSpinBox;
    51. howManyDigits_SB->setRange(2,4);
    52. howManyDigits_SB->setValue(2);
    53. howManyDigits_SB->setEnabled(false);
    54. startingDigit_L = new QLabel(tr("Cyfra poczatkowa"));
    55. startingDigit_SB = new QSpinBox;
    56. startingDigit_SB->setRange(1,MAX_START_NUMBER);
    57. startingDigit_SB->setValue(1);
    58. elasticAmountOfDigits_RB->setChecked(true);
    59. connect(elasticAmountOfDigits_RB, SIGNAL(clicked(bool)), this, SLOT(resetSpinRange(bool)));
    60. connect(howManyDigits_SB, SIGNAL(valueChanged(int)), this, SLOT(changeSpinRange(int)));
    61. }
    62.  
    63. void RenamerWidget::checkInput(void) {
    64. changeNames_Button->setEnabled(chain_LE->hasAcceptableInput());
    65. }
    66.  
    67. void RenamerWidget::constDigitsSpin(bool bl) {
    68. howManyDigits_SB->setEnabled(bl);
    69. }
    70.  
    71. void RenamerWidget::resetSpinRange(bool bl) {
    72. if (bl==true) startingDigit_SB->setRange(1,MAX_START_NUMBER);
    73. }
    74.  
    75. void RenamerWidget::changeSpinRange(int it) {
    76. switch (it) {
    77. case 2: startingDigit_SB->setRange(1,98);
    78. break;
    79. case 3: startingDigit_SB->setRange(1,998);
    80. break;
    81. case 4: startingDigit_SB->setRange(1,9998);
    82. break;
    83. default: startingDigit_SB->setRange(1,98);
    84. }
    85. startingDigit_SB->setValue(1);
    86. }
    87.  
    88.  
    89. void RenamerWidget::arranging(void) {
    90. newName_GB = new QGroupBox(tr("Nowa nazwa"));
    91. numerationStyle_GB = new QGroupBox(tr("Sposob numeracji"));
    92.  
    93. QVBoxLayout *mainL = new QVBoxLayout;
    94.  
    95. newName_GB->setLayout(v1L);
    96. mainL->addLayout(h1L);
    97. mainL->addWidget(newName_GB);
    98. mainL->addLayout(h4L);
    99.  
    100.  
    101. v1L->addLayout(h2L);
    102. v1L->addLayout(h3L);
    103.  
    104. h1L->addWidget(chooseFiles_Button);
    105. h1L->addWidget(howManyFiles_L);
    106.  
    107. h4L->addWidget(changeNames_Button);
    108. h4L->addWidget(exit_Button);
    109. /*
    110. h2L->addWidget(lineEditDescription_L);
    111. h2L->addWidget(chain_LE);
    112. */
    113. setLayout(mainL);
    114. }
    115.  
    116.  
    117. void RenamerWidget::alterNames(void) {
    118.  
    119. }
    120.  
    121. void RenamerWidget::selectFiles(void) {
    122.  
    123. }
    To copy to clipboard, switch view to plain text mode 

    and there is main.cpp

    Qt Code:
    1. #include <QApplication>
    2. #include "renamer.h"
    3.  
    4. int main(int argc, char *argv[]) {
    5. QApplication app(argc, argv);
    6. RenamerWidget window;
    7. return app.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 

    I'm well aware that there is some text in Polish and the great deal of you won't understand it, but what message that text conveys is not crucial here.

    Best wishes,
    Mike

  2. #2
    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: Simple problem with layouts and maybe QGroupBox. Segmentation Fault

    That is because "lineEditDescription_L" is not created anywhere, just used.
    I don't see it created in initialization( or in other part of the code you posted ).

    Do you create this object in a part of the code that you did not post?
    If not try creating it and uncomment those lines.
    Should work.

  3. #3
    Join Date
    May 2007
    Location
    Warsaw, Poland
    Posts
    52
    Thanks
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Simple problem with layouts and maybe QGroupBox. Segmentation Fault

    My mind boggles that I hasn't spotted it! I'm really glad of your helping hand.

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.