PDA

View Full Version : SIGSESV error



cipher1729
22nd June 2011, 02:08
I'm trying to create a simple binary to decimal to hexadecimal appplication..
My code seems to be compiling but each time..I'm getting a SIGSESV error. What am I doing wrong? Thanks.


main.cpp


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

int main(int argc,char* argv[])
{
QApplication a(argc,argv);

ByteConvertorDialog c;
c.setAttribute(Qt::WA_QuitOnClose);
c.show();

return a.exec();
}

newByte.cpp


#include "ByteConvertorDialog.h"
#include<QLabel>
#include<QLineEdit>
#include<QPushButton>
#include<QGridLayout>
#include<QVBoxLayout>
#include<QHBoxLayout>

ByteConvertorDialog::ByteConvertorDialog()
{
QVBoxLayout * mainLayout = new QVBoxLayout(this);
QGridLayout * editLayout = new QGridLayout;
QHBoxLayout * pushButton = new QHBoxLayout;

mainLayout -> addLayout(editLayout);
mainLayout -> addStretch();
mainLayout ->addLayout(pushButton);

QLabel * bin = new QLabel("Binary");
QLabel * dec = new QLabel("Decimal");
QLabel * hex = new QLabel("Hexadecimal");
decEdit = new QLineEdit;
binEdit = new QLineEdit;
hexEdit = new QLineEdit;
QPushButton *button = new QPushButton("Quit");

editLayout ->addWidget(bin,0,0);
editLayout ->addWidget(dec,1,0);
editLayout ->addWidget(hex,2,0);
editLayout ->addWidget(binEdit,0,1);
editLayout ->addWidget(decEdit,1,1);
editLayout ->addWidget(hexEdit,2,1);

pushButton ->addStretch();
pushButton ->addWidget(button);

button ->setDefault(true);

connect(button,SIGNAL(clicked()),this,SLOT(accept( )));
connect(decEdit,SIGNAL(textChanged(const QString&)),this,SLOT(decChanged(const QString&)));
connect(binEdit,SIGNAL(textChanged(const QString&)),this,SLOT(binChanged(const QString&)));
connect(hexEdit,SIGNAL(textChanged(const QString&)),this,SLOT(hexChanged(const QString&)));

}

void ByteConvertorDialog::decChanged(const QString &newValue)
{
bool ok;
int num= newValue.toInt(&ok);
binEdit->setText(QString::number(num,2));
hexEdit->setText(QString::number(num,16));

}
void ByteConvertorDialog::binChanged(const QString &newValue)
{
bool ok;
int num= newValue.toInt(&ok);
decEdit->setText(QString::number(num,10));
hexEdit->setText(QString::number(num,16));

}
void ByteConvertorDialog::hexChanged(const QString &newValue)
{
bool ok;
int num= newValue.toInt(&ok);
binEdit->setText(QString::number(num,2));
decEdit->setText(QString::number(num,10));

}


byteConvertorDialog.h


#ifndef BYTECONVERTORDIALOG_H
#define BYTECONVERTORDIALOG_H
#include<QObject>
#include<QDialog>
#include<QLineEdit>
class QLineEdit;

class ByteConvertorDialog:public QDialog
{ Q_OBJECT
public:
ByteConvertorDialog();
private:
QLineEdit * decEdit;
QLineEdit * binEdit;
QLineEdit * hexEdit ;
private slots:
void decChanged(const QString&);
void binChanged(const QString&);
void hexChanged(const QString&);
};
#endif // BYTECONVERTORDIALOG_H

ChrisW67
22nd June 2011, 02:43
At a guess, when you programatically change the values inside your handler slots you trigger a cascade of signals. Perhaps you want the QLineEdit::textEdited() signal instead.

wysota
22nd June 2011, 02:50
If the calculations are correct, the signals will not be emitted as changing text to the same text doesn't cause textChanged() signal to be emitted.

@cipher1729: Use a debugger to see where your code crashes.

cipher1729
22nd June 2011, 03:11
Hi..
Changing textChanged to textEdited solved the problem..
In all probability calling setText also caused textChanged to be called..so there was a cascade of signals..
Thanks:D to both the repliers

ChrisW67
22nd June 2011, 03:21
With the code as is the edit boxes do not function correctly before it crashes. I put a debug statement at the top of each slot: and started the program:


// Press 1 in binary box
binChanged "1"
decChanged "1"
hexChanged "1"
// 1 displayed in all boxes
// Press 1 in binary box
binChanged "11"
decChanged "11"
binChanged "1011"
decChanged "1011"
binChanged "1111110011"
decChanged "1111110011"
binChanged "1000010001110100011000101111011"
decChanged "0"
binChanged "0"
hexChanged "0"
hexChanged "423a317b"
hexChanged "3f3"
hexChanged "b"
// 0 displayed in all boxes

Clearly not the expected result. Press 2 in the binary box and and a stream of repeating debug messages ensues followed by:


Program received signal SIGSEGV, Segmentation fault.
0xb792381a in QTextEngine::itemize() const () from /usr/lib/qt4/libQtGui.so.4
(gdb) bt
#0 0xb792381a in QTextEngine::itemize() const ()
from /usr/lib/qt4/libQtGui.so.4
#1 0xb79256a3 in QTextEngine::attributes() const ()
from /usr/lib/qt4/libQtGui.so.4
#2 0xb792dbb1 in QTextLine::layout_helper(int) ()
from /usr/lib/qt4/libQtGui.so.4
#3 0xb792fc50 in QTextLine::setNumColumns(int) ()
from /usr/lib/qt4/libQtGui.so.4
#4 0xb792fcd1 in QTextLayout::endLayout() () from /usr/lib/qt4/libQtGui.so.4
#5 0xb7afa381 in QLineControl::updateDisplayText(bool) ()
from /usr/lib/qt4/libQtGui.so.4
#6 0xb7afc780 in QLineControl::finishChange(int, bool, bool) ()
from /usr/lib/qt4/libQtGui.so.4
#7 0xb7afcada in QLineControl::internalSetText(QString const&, int, bool) ()
from /usr/lib/qt4/libQtGui.so.4
#8 0xb7af2f7d in QLineEdit::setText(QString const&) ()
from /usr/lib/qt4/libQtGui.so.4
#9 0x0804b959 in ByteConvertorDialog::binChanged (this=0xbfffe978,
newValue=...) at ByteConvertorDialog.cpp:62


The conversions from string to int don't specify the base... this is the problem because it keeps changing the value because of incorrect conversions.

wysota
22nd June 2011, 03:21
Are you getting proper values? Because your code is definitely wrong. You are ignoring the number base and thus you get bogus values and thus you get a cascade of signals upon textChanged. As soon as you enter some other value than 0 or 1, your code will become unstable.

cipher1729
22nd June 2011, 04:44
I'm following a book..as according to the example given..I put in validators to restrict the values entered in the line edits. But..yes..something's still wrong..the conversion is flawed. Like..decimal to binary works fine but not vice-versa.



#include "ByteConvertorDialog.h"
#include<QLabel>
#include<QLineEdit>
#include<QPushButton>
#include<QGridLayout>
#include<QVBoxLayout>
#include<QHBoxLayout>
#include<QValidator>

ByteConvertorDialog::ByteConvertorDialog()
{
QVBoxLayout * mainLayout = new QVBoxLayout(this);
QGridLayout * editLayout = new QGridLayout;
QHBoxLayout * pushButton = new QHBoxLayout;

mainLayout -> addLayout(editLayout);
mainLayout -> addStretch();
mainLayout ->addLayout(pushButton);

QLabel * bin = new QLabel("Binary");
QLabel * dec = new QLabel("Decimal");
QLabel * hex = new QLabel("Hexadecimal");
decEdit = new QLineEdit;
binEdit = new QLineEdit;
hexEdit = new QLineEdit;
QPushButton *button = new QPushButton("Quit");

editLayout ->addWidget(bin,0,0);
editLayout ->addWidget(dec,1,0);
editLayout ->addWidget(hex,2,0);
editLayout ->addWidget(binEdit,0,1);
editLayout ->addWidget(decEdit,1,1);
editLayout ->addWidget(hexEdit,2,1);

pushButton ->addStretch();
pushButton ->addWidget(button);

button ->setDefault(true);
QIntValidator* decValidator = new QIntValidator(0,255,decEdit);
decEdit->setValidator(decValidator);
QRegExpValidator* hexValidator = new QRegExpValidator(QRegExp("[0-9A-Fa-f]{1,2}"),hexEdit);
hexEdit->setValidator(hexValidator);
QRegExpValidator*binValidator = new QRegExpValidator(QRegExp("[01]{1,8}"),binEdit);
binEdit->setValidator(binValidator);

connect(button,SIGNAL(clicked()),this,SLOT(accept( )));
connect(decEdit,SIGNAL(textEdited(const QString&)),this,SLOT(decChanged(const QString&)));
connect(binEdit,SIGNAL(textEdited(const QString&)),this,SLOT(binChanged(const QString&)));
connect(hexEdit,SIGNAL(textEdited(const QString&)),this,SLOT(hexChanged(const QString&)));

}

void ByteConvertorDialog::decChanged(const QString &newValue)
{
bool ok;
int num= newValue.toInt(&ok);
if (ok) {
hexEdit->setText(QString::number(num, 16));
binEdit->setText(QString::number(num, 2));
} else {
hexEdit->setText("");
binEdit->setText("");
}

}
void ByteConvertorDialog::binChanged(const QString &newValue)
{
bool ok;
int num= newValue.toInt(&ok);
if (ok) {
hexEdit->setText(QString::number(num, 16));
decEdit->setText(QString::number(num));
} else {
hexEdit->setText("");
decEdit->setText("");
}

}
void ByteConvertorDialog::hexChanged(const QString &newValue)
{
bool ok;
int num= newValue.toInt(&ok);
if (ok) {
decEdit->setText(QString::number(num, 10));
binEdit->setText(QString::number(num, 2));
} else {
decEdit->setText("");
binEdit->setText("");
}

}

ChrisW67
22nd June 2011, 04:58
You need to pass the optional second argument to QString::toInt() to specify the base you are converting from.

cipher1729
22nd June 2011, 05:04
That seems to have done it. thanks