PDA

View Full Version : need help in qt code



doforumda
27th August 2010, 11:58
hi

i am following "The Book Of Qt 4 The art of Building Qt Applications" and trying the following example which converts byte to decimal, hexadecimal and binary but when i run, it produces error

here is the code
byteconverterdialog.h file


#ifndef BYTECONVERTERDIALOG_H
#define BYTECONVERTERDIALOG_H

#include <QDialog>

class byteConverterDialog : public QDialog
{
Q_OBJECT

public:
byteConverterDialog();
};

#endif // BYTECONVERTERDIALOG_H


byteconverter.h


#ifndef BYTECONVERTER_H
#define BYTECONVERTER_H

#include <QObject>

class ByteConverter : public QObject
{
Q_OBJECT

public:
ByteConverter(QObject* = 0);

public slots:
void setDec(const QString &);
void setHex(const QString &);
void setBin(const QString &);

signals:
void decChanged(const QString&);
void hexChanged(const QString&);
void binChanged(const QString&);
};

#endif // BYTECONVERTER_H


byteconverterdialog.cpp


#include "byteconverterdialog.h"
#include "byteconverter.h"
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QIntValidator>

byteConverterDialog::byteConverterDialog()
{
//generate the necessary layouts
QVBoxLayout *mainLayout = new QVBoxLayout(this);
QGridLayout *editLayout = new QGridLayout;
QHBoxLayout *buttonLayout = new QHBoxLayout;

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

//generate the labels and line-edits and add them to the object pointed at by editLayout
QLabel *decLabel = new QLabel(tr("Decimal"));
QLabel *hexLabel = new QLabel(tr("hexadecimal"));
QLabel *binLabel = new QLabel(tr("Binary"));

decEdit = new QLineEdit;
hexEdit = new QLineEdit;
binEdit = new QLineEdit;

editLayout->addWidget(decLabel, 0, 0);
editLayout->addWidget(decEdit, 0, 1);
editLayout->addWidget(hexLabel, 1, 0);
editLayout->addWidget(hexEdit, 1, 1);
editLayout->addWidget(binLabel, 2, 0);
editLayout->addWidget(binEdit, 2, 1);

//create the quit button and add it to the object pointed at by buttonLayout.
QPushButton *exitButton = new QPushButton(tr("Quit"));

buttonLayout->addStretch();
buttonLayout->addWidget(exitButton);
exitButton->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);

setWindowTitle(tr("Byte Converter"));

connect(exitButton, SIGNAL(clicked()), this, SLOT(accept()));

ByteConverter* bc = new ByteConverter(this);

connect(decEdit, SIGNAL(textChanged(const QString &)), bc, SLOT(setDec(const QString &)));
connect(hexEdit, SIGNAL(textChanged(const QString &)), bc, SLOT(setHex(const QString &)));
connect(binEdit, SIGNAL(textChanged(const QString &)), bc, SLOT(setBin(const QString &)));

connect(bc, SIGNAL(decChanged(const QString&)), decEdit, SLOT(setText(const QString&)));
connect(bc, SIGNAL(hexChanged(const QString&)), hexEdit, SLOT(setText(const QString&)));
connect(bc, SIGNAL(binChanged(const QString&)), binEdit, SLOT(setText(const QString&)));
}

void byteConverterDialog::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 byteConverterDialog::hexChanged(const QString& newValue)
{
bool ok;
int num = newValue.toInt(&ok);
if(ok) {
decEdit->setText(QString::number(num));
binEdit->setText(QString::number(num, 2));
}
else {
decEdit->setText("");
binEdit->setText("");
}
}

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


byteconverter.cpp


#include "byteconverter.h"

ByteConverter::ByteConverter(QObject* parent) : QObject(parent)
{
}

void ByteConverter::setDec(const QString& newValue)
{
bool ok;
int num = newValue.toInt(&ok);
if(ok) {
emit hexChanged(QString::number(num, 16));
emit binChanged(QString::number(num, 2));
}
else {
emit hexChanged("");
emit binChanged("");
}
}

void ByteConverter::setHex(const QString& newValue)
{
bool ok;
int num = newValue.toInt(&ok, 16);
if(ok) {
emit decChanged(QString::number(num));
emit binChanged(QString::number(num, 2));
}
else {
emit decChanged("");
emit binChanged("");
}
}

void ByteConverter::setBin(const QString& newValue)
{
bool ok;
int num = newValue.toInt(&ok, 2);
if(ok) {
emit decChanged(QString::number(num));
emit hexChanged(QString::number(num, 16));
}
else {
emit decChanged("");
emit hexChanged("");
}
}


main.cpp


#include <QtGui/QApplication>
#include "byteconverterdialog.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
byteConverterDialog bc;
bc.setAttribute(Qt::WA_QuitOnClose);
bc.show();

return a.exec();
}


when i run this code, it displays these errors



decEdit was not declared in this scope byteconverterdialog.cpp 27
hexEdit was not declared in this scope byteconverterdialog.cpp 28
binEdit was not declared in this scope byteconverterdialog.cpp 29

void byteConverterDialog::decChanged(const QString&) member function declared in class byteConverterDialog byteconverterdialog.cpp 70
void byteConverterDialog::hexChanged(const QString&) member function declared in class byteConverterDialog byteconverterdialog.cpp 84
void byteConverterDialog::binChanged(const QString&) member function declared in class byteConverterDialog byteconverterdialog.cpp 98

aamer4yu
27th August 2010, 12:13
As the error says... you have not declared decEdit,hexEdit and binEdit...
Something like QLineEdit * decEdit = new QLineEdit;
or m_decEdit = new QLineEdit; // Where m_decEdit is member variable.

doforumda
27th August 2010, 12:24
in which class should i declare these?

doforumda
27th August 2010, 14:05
this is what i got in Compile Output tab


Running build steps for project byteConverter...
Configuration unchanged, skipping qmake step.
Starting: "C:/Qt/2010.04/mingw/bin/mingw32-make.exe" -w
mingw32-make: Entering directory `C:/Users/Administrator/Documents/byteConverter-build-desktop'
C:/Qt/2010.04/mingw/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory `C:/Users/Administrator/Documents/byteConverter-build-desktop'
g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\..\..\..\Qt\2010.04\qt\include\QtCore" -I"..\..\..\..\Qt\2010.04\qt\include\QtGui" -I"..\..\..\..\Qt\2010.04\qt\include" -I"..\..\..\..\Qt\2010.04\qt\include\ActiveQt" -I"debug" -I"." -I"..\byteConverter" -I"." -I"..\..\..\..\Qt\2010.04\qt\mkspecs\win32-g++" -o debug\byteconverterdialog.o ..\byteConverter\byteconverterdialog.cpp
mingw32-make[1]: Leaving directory `C:/Users/Administrator/Documents/byteConverter-build-desktop'
mingw32-make: Leaving directory `C:/Users/Administrator/Documents/byteConverter-build-desktop'
..\byteConverter\byteconverterdialog.cpp: In constructor 'byteConverterDialog::byteConverterDialog()':
..\byteConverter\byteconverterdialog.cpp:27: error: 'm_decEdit' was not declared in this scope
..\byteConverter\byteconverterdialog.cpp:28: error: 'hexEdit' was not declared in this scope
..\byteConverter\byteconverterdialog.cpp:29: error: 'binEdit' was not declared in this scope
..\byteConverter\byteconverterdialog.cpp:32: error: 'decEdit' was not declared in this scope
..\byteConverter\byteconverterdialog.cpp: At global scope:
..\byteConverter\byteconverterdialog.cpp:70: error: no 'void byteConverterDialog::decChanged(const QString&)' member function declared in class 'byteConverterDialog'
..\byteConverter\byteconverterdialog.cpp:84: error: no 'void byteConverterDialog::hexChanged(const QString&)' member function declared in class 'byteConverterDialog'
..\byteConverter\byteconverterdialog.cpp:98: error: no 'void byteConverterDialog::binChanged(const QString&)' member function declared in class 'byteConverterDialog'
mingw32-make[1]: *** [debug/byteconverterdialog.o] Error 1
mingw32-make: *** [debug] Error 2
The process "C:/Qt/2010.04/mingw/bin/mingw32-make.exe" exited with code %2.
Error while building project byteConverter (target: Desktop)
When executing build step 'Make'

aamer4yu
27th August 2010, 14:20
A variable needs to be of some type..
you cannot simply write
xyz = something; You need to define what type of is xyz, is it int, QString, QWidget etc...

tbscope
27th August 2010, 14:27
To answer your question of where to add it:


#ifndef BYTECONVERTERDIALOG_H
#define BYTECONVERTERDIALOG_H

#include <QDialog>

class byteConverterDialog : public QDialog
{
Q_OBJECT

public:
byteConverterDialog();

private:
// <----------Here.
};

#endif // BYTECONVERTERDIALOG_H