PDA

View Full Version : Simple problem with layouts and maybe QGroupBox. Segmentation Fault



mtrpoland
19th May 2007, 12:20
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


#ifndef RENAMER_CLASS_DEFINITION_SAFETY_DECLARATION
#define RENAMER_CLASS_DEFINITION_SAFETY_DECLARATION
//deklaracja klasy RenamerWidget
#include <QWidget>
#include <QObject>

class QPushButton;
class QLabel;
class QRadioButton;
class QGroupBox;
class QLineEdit;
class QButtonGroup;
class QSpinBox;
class QRegExp;
class QValidator;

class RenamerWidget : public QWidget {
Q_OBJECT
public:
RenamerWidget(QWidget *parent = 0);
private slots:
void initialization(void);
void arranging(void);
void selectFiles(void); // Button "Choose Files"
void alterNames(void); // Button "Zmien nazwy"
void checkInput(void); // decyduje czy przycisk "Zmien nazwy" jest dostepny
void constDigitsSpin(bool); // decyduje czy spin przy radio buttonie jest dostepny
void resetSpinRange(bool); // zmienia na zakres domyslny spina cyfr poczatkowej
void changeSpinRange(int); // zmienia zakres spina zaleznie od wybranej wartosci w 1 spinie
private:
static const int MAX_START_NUMBER = 999;
QPushButton *chooseFiles_Button;
QPushButton *exit_Button;
QPushButton *changeNames_Button;
QLabel *howManyFiles_L;
QGroupBox *newName_GB;
QGroupBox *numerationStyle_GB;
QLabel *lineEditDescription_L;
QLineEdit *chain_LE;
QRadioButton *constantAmountOfDigits_RB;
QRadioButton *elasticAmountOfDigits_RB;
QButtonGroup *radioGroup_BG;
QSpinBox *howManyDigits_SB;
QLabel *startingDigit_L;
QSpinBox *startingDigit_SB;
QRegExp *rx;
QValidator *validator;
};
#endif



File renamer.cpp


//definicja klasy renamer
#include "renamer.h"
#include <QPushButton>
#include <QApplication>
#include <QLabel>
#include <QRadioButton>
#include <QGroupBox>
#include <QLineEdit>
#include <QButtonGroup>
#include <QSpinBox>
#include <QRegExp>
#include <QValidator>
#include <QGridLayout>

#include <QVBoxLayout>
#include <QHBoxLayout>

RenamerWidget::RenamerWidget(QWidget *parent) : QWidget(parent) {
setWindowTitle(tr("MTRenaMeR"));
setFixedSize(500,300);
initialization();
qWarning("initialization();");
arranging();
qWarning("arranging();");
show();

}

void RenamerWidget::initialization(void) {
chooseFiles_Button = new QPushButton(tr("Wybierz pliki"));
exit_Button = new QPushButton(tr("Wyjdz"));
changeNames_Button = new QPushButton(tr("Zmien nazwy"));
changeNames_Button->setEnabled(false);
connect(exit_Button, SIGNAL(clicked()), qApp, SLOT(quit()));
connect(changeNames_Button, SIGNAL(clicked()), this, SLOT(alterNames()));
connect(chooseFiles_Button, SIGNAL(clicked()), this, SLOT(selectFiles()));
howManyFiles_L = new QLabel(tr("Nie wybrano zadnych plikow."));
chain_LE = new QLineEdit;
rx = new QRegExp("[A-Za-z0-9]{1,25}_");
validator = new QRegExpValidator(*rx,0);
chain_LE->setValidator(validator);
connect(chain_LE, SIGNAL(textChanged(const QString &)), this, SLOT(checkInput()));

constantAmountOfDigits_RB = new QRadioButton(tr("Stala ilosc cyfr"));
elasticAmountOfDigits_RB = new QRadioButton(tr("Zmienna ilosc cyfr"));
radioGroup_BG = new QButtonGroup;
radioGroup_BG->addButton(constantAmountOfDigits_RB,1);
radioGroup_BG->addButton(elasticAmountOfDigits_RB,2);
connect(constantAmountOfDigits_RB, SIGNAL(clicked(bool)), this, SLOT(constDigitsSpin(bool)));
howManyDigits_SB = new QSpinBox;
howManyDigits_SB->setRange(2,4);
howManyDigits_SB->setValue(2);
howManyDigits_SB->setEnabled(false);
startingDigit_L = new QLabel(tr("Cyfra poczatkowa"));
startingDigit_SB = new QSpinBox;
startingDigit_SB->setRange(1,MAX_START_NUMBER);
startingDigit_SB->setValue(1);
elasticAmountOfDigits_RB->setChecked(true);
connect(elasticAmountOfDigits_RB, SIGNAL(clicked(bool)), this, SLOT(resetSpinRange(bool)));
connect(howManyDigits_SB, SIGNAL(valueChanged(int)), this, SLOT(changeSpinRange(int)));
}

void RenamerWidget::checkInput(void) {
changeNames_Button->setEnabled(chain_LE->hasAcceptableInput());
}

void RenamerWidget::constDigitsSpin(bool bl) {
howManyDigits_SB->setEnabled(bl);
}

void RenamerWidget::resetSpinRange(bool bl) {
if (bl==true) startingDigit_SB->setRange(1,MAX_START_NUMBER);
}

void RenamerWidget::changeSpinRange(int it) {
switch (it) {
case 2: startingDigit_SB->setRange(1,98);
break;
case 3: startingDigit_SB->setRange(1,998);
break;
case 4: startingDigit_SB->setRange(1,9998);
break;
default: startingDigit_SB->setRange(1,98);
}
startingDigit_SB->setValue(1);
}


void RenamerWidget::arranging(void) {
newName_GB = new QGroupBox(tr("Nowa nazwa"));
numerationStyle_GB = new QGroupBox(tr("Sposob numeracji"));

QVBoxLayout *mainL = new QVBoxLayout;
QVBoxLayout *v1L = new QVBoxLayout;
QHBoxLayout *h1L = new QHBoxLayout;
QHBoxLayout *h2L = new QHBoxLayout;
QHBoxLayout *h3L = new QHBoxLayout;
QHBoxLayout *h4L = new QHBoxLayout;

newName_GB->setLayout(v1L);
mainL->addLayout(h1L);
mainL->addWidget(newName_GB);
mainL->addLayout(h4L);


v1L->addLayout(h2L);
v1L->addLayout(h3L);

h1L->addWidget(chooseFiles_Button);
h1L->addWidget(howManyFiles_L);

h4L->addWidget(changeNames_Button);
h4L->addWidget(exit_Button);
/*
h2L->addWidget(lineEditDescription_L);
h2L->addWidget(chain_LE);
*/
setLayout(mainL);
}


void RenamerWidget::alterNames(void) {

}

void RenamerWidget::selectFiles(void) {

}



and there is main.cpp


#include <QApplication>
#include "renamer.h"

int main(int argc, char *argv[]) {
QApplication app(argc, argv);
RenamerWidget window;
return app.exec();
}

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

marcel
19th May 2007, 15:06
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.

mtrpoland
19th May 2007, 19:25
My mind boggles that I hasn't spotted it! I'm really glad of your helping hand.