Results 1 to 6 of 6

Thread: need help in qt code

  1. #1
    Join Date
    Jul 2010
    Posts
    18
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    Windows

    Default need help in qt code

    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
    Qt Code:
    1. #ifndef BYTECONVERTERDIALOG_H
    2. #define BYTECONVERTERDIALOG_H
    3.  
    4. #include <QDialog>
    5.  
    6. class byteConverterDialog : public QDialog
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. byteConverterDialog();
    12. };
    13.  
    14. #endif // BYTECONVERTERDIALOG_H
    To copy to clipboard, switch view to plain text mode 

    byteconverter.h
    Qt Code:
    1. #ifndef BYTECONVERTER_H
    2. #define BYTECONVERTER_H
    3.  
    4. #include <QObject>
    5.  
    6. class ByteConverter : public QObject
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. ByteConverter(QObject* = 0);
    12.  
    13. public slots:
    14. void setDec(const QString &);
    15. void setHex(const QString &);
    16. void setBin(const QString &);
    17.  
    18. signals:
    19. void decChanged(const QString&);
    20. void hexChanged(const QString&);
    21. void binChanged(const QString&);
    22. };
    23.  
    24. #endif // BYTECONVERTER_H
    To copy to clipboard, switch view to plain text mode 

    byteconverterdialog.cpp
    Qt Code:
    1. #include "byteconverterdialog.h"
    2. #include "byteconverter.h"
    3. #include <QLabel>
    4. #include <QLineEdit>
    5. #include <QPushButton>
    6. #include <QVBoxLayout>
    7. #include <QHBoxLayout>
    8. #include <QGridLayout>
    9. #include <QIntValidator>
    10.  
    11. byteConverterDialog::byteConverterDialog()
    12. {
    13. //generate the necessary layouts
    14. QVBoxLayout *mainLayout = new QVBoxLayout(this);
    15. QGridLayout *editLayout = new QGridLayout;
    16. QHBoxLayout *buttonLayout = new QHBoxLayout;
    17.  
    18. mainLayout->addLayout(editLayout);
    19. mainLayout->addStretch();
    20. mainLayout->addLayout(buttonLayout);
    21.  
    22. //generate the labels and line-edits and add them to the object pointed at by editLayout
    23. QLabel *decLabel = new QLabel(tr("Decimal"));
    24. QLabel *hexLabel = new QLabel(tr("hexadecimal"));
    25. QLabel *binLabel = new QLabel(tr("Binary"));
    26.  
    27. decEdit = new QLineEdit;
    28. hexEdit = new QLineEdit;
    29. binEdit = new QLineEdit;
    30.  
    31. editLayout->addWidget(decLabel, 0, 0);
    32. editLayout->addWidget(decEdit, 0, 1);
    33. editLayout->addWidget(hexLabel, 1, 0);
    34. editLayout->addWidget(hexEdit, 1, 1);
    35. editLayout->addWidget(binLabel, 2, 0);
    36. editLayout->addWidget(binEdit, 2, 1);
    37.  
    38. //create the quit button and add it to the object pointed at by buttonLayout.
    39. QPushButton *exitButton = new QPushButton(tr("Quit"));
    40.  
    41. buttonLayout->addStretch();
    42. buttonLayout->addWidget(exitButton);
    43. exitButton->setDefault(true);
    44.  
    45. QIntValidator* decValidator =
    46. new QIntValidator(0, 255, decEdit);
    47. decEdit->setValidator(decValidator);
    48. QRegExpValidator* hexValidator =
    49. new QRegExpValidator(QRegExp("[0-9A-Fa-f]{1,2}"), hexEdit);
    50. hexEdit->setValidator(hexValidator);
    51. QRegExpValidator* binValidator =
    52. new QRegExpValidator(QRegExp("[01]{1,8}"), binEdit);
    53. binEdit->setValidator(binValidator);
    54.  
    55. setWindowTitle(tr("Byte Converter"));
    56.  
    57. connect(exitButton, SIGNAL(clicked()), this, SLOT(accept()));
    58.  
    59. ByteConverter* bc = new ByteConverter(this);
    60.  
    61. connect(decEdit, SIGNAL(textChanged(const QString &)), bc, SLOT(setDec(const QString &)));
    62. connect(hexEdit, SIGNAL(textChanged(const QString &)), bc, SLOT(setHex(const QString &)));
    63. connect(binEdit, SIGNAL(textChanged(const QString &)), bc, SLOT(setBin(const QString &)));
    64.  
    65. connect(bc, SIGNAL(decChanged(const QString&)), decEdit, SLOT(setText(const QString&)));
    66. connect(bc, SIGNAL(hexChanged(const QString&)), hexEdit, SLOT(setText(const QString&)));
    67. connect(bc, SIGNAL(binChanged(const QString&)), binEdit, SLOT(setText(const QString&)));
    68. }
    69.  
    70. void byteConverterDialog::decChanged(const QString& newValue)
    71. {
    72. bool ok;
    73. int num = newValue.toInt(&ok);
    74. if(ok) {
    75. hexEdit->setText(QString::number(num, 16));
    76. binEdit->setText(QString::number(num, 2));
    77. }
    78. else {
    79. hexEdit->setText("");
    80. binEdit->setText("");
    81. }
    82. }
    83.  
    84. void byteConverterDialog::hexChanged(const QString& newValue)
    85. {
    86. bool ok;
    87. int num = newValue.toInt(&ok);
    88. if(ok) {
    89. decEdit->setText(QString::number(num));
    90. binEdit->setText(QString::number(num, 2));
    91. }
    92. else {
    93. decEdit->setText("");
    94. binEdit->setText("");
    95. }
    96. }
    97.  
    98. void byteConverterDialog::binChanged(const QString& newValue)
    99. {
    100. bool ok;
    101. int num = newValue.toInt(&ok);
    102. if(ok) {
    103. decEdit->setText(QString::number(num));
    104. hexEdit->setText(QString::number(num, 16));
    105. }
    106. else {
    107. hexEdit->setText("");
    108. decEdit->setText("");
    109. }
    110. }
    To copy to clipboard, switch view to plain text mode 

    byteconverter.cpp
    Qt Code:
    1. #include "byteconverter.h"
    2.  
    3. ByteConverter::ByteConverter(QObject* parent) : QObject(parent)
    4. {
    5. }
    6.  
    7. void ByteConverter::setDec(const QString& newValue)
    8. {
    9. bool ok;
    10. int num = newValue.toInt(&ok);
    11. if(ok) {
    12. emit hexChanged(QString::number(num, 16));
    13. emit binChanged(QString::number(num, 2));
    14. }
    15. else {
    16. emit hexChanged("");
    17. emit binChanged("");
    18. }
    19. }
    20.  
    21. void ByteConverter::setHex(const QString& newValue)
    22. {
    23. bool ok;
    24. int num = newValue.toInt(&ok, 16);
    25. if(ok) {
    26. emit decChanged(QString::number(num));
    27. emit binChanged(QString::number(num, 2));
    28. }
    29. else {
    30. emit decChanged("");
    31. emit binChanged("");
    32. }
    33. }
    34.  
    35. void ByteConverter::setBin(const QString& newValue)
    36. {
    37. bool ok;
    38. int num = newValue.toInt(&ok, 2);
    39. if(ok) {
    40. emit decChanged(QString::number(num));
    41. emit hexChanged(QString::number(num, 16));
    42. }
    43. else {
    44. emit decChanged("");
    45. emit hexChanged("");
    46. }
    47. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "byteconverterdialog.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. byteConverterDialog bc;
    8. bc.setAttribute(Qt::WA_QuitOnClose);
    9. bc.show();
    10.  
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    when i run this code, it displays these errors

    Qt Code:
    1. decEdit was not declared in this scope byteconverterdialog.cpp 27
    2. hexEdit was not declared in this scope byteconverterdialog.cpp 28
    3. binEdit was not declared in this scope byteconverterdialog.cpp 29
    4.  
    5. void byteConverterDialog::decChanged(const QString&) member function declared in class byteConverterDialog byteconverterdialog.cpp 70
    6. void byteConverterDialog::hexChanged(const QString&) member function declared in class byteConverterDialog byteconverterdialog.cpp 84
    7. void byteConverterDialog::binChanged(const QString&) member function declared in class byteConverterDialog byteconverterdialog.cpp 98
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: need help in qt code

    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.

  3. #3
    Join Date
    Jul 2010
    Posts
    18
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    Windows

    Default Re: need help in qt code

    in which class should i declare these?

  4. #4
    Join Date
    Jul 2010
    Posts
    18
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi PyQt3 PyQt4
    Platforms
    Windows

    Default Re: need help in qt code

    this is what i got in Compile Output tab
    Qt Code:
    1. Running build steps for project byteConverter...
    2. Configuration unchanged, skipping qmake step.
    3. Starting: "C:/Qt/2010.04/mingw/bin/mingw32-make.exe" -w
    4. mingw32-make: Entering directory `C:/Users/Administrator/Documents/byteConverter-build-desktop'
    5. C:/Qt/2010.04/mingw/bin/mingw32-make -f Makefile.Debug
    6. mingw32-make[1]: Entering directory `C:/Users/Administrator/Documents/byteConverter-build-desktop'
    7. 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
    8. mingw32-make[1]: Leaving directory `C:/Users/Administrator/Documents/byteConverter-build-desktop'
    9. mingw32-make: Leaving directory `C:/Users/Administrator/Documents/byteConverter-build-desktop'
    10. ..\byteConverter\byteconverterdialog.cpp: In constructor 'byteConverterDialog::byteConverterDialog()':
    11. ..\byteConverter\byteconverterdialog.cpp:27: error: 'm_decEdit' was not declared in this scope
    12. ..\byteConverter\byteconverterdialog.cpp:28: error: 'hexEdit' was not declared in this scope
    13. ..\byteConverter\byteconverterdialog.cpp:29: error: 'binEdit' was not declared in this scope
    14. ..\byteConverter\byteconverterdialog.cpp:32: error: 'decEdit' was not declared in this scope
    15. ..\byteConverter\byteconverterdialog.cpp: At global scope:
    16. ..\byteConverter\byteconverterdialog.cpp:70: error: no 'void byteConverterDialog::decChanged(const QString&)' member function declared in class 'byteConverterDialog'
    17. ..\byteConverter\byteconverterdialog.cpp:84: error: no 'void byteConverterDialog::hexChanged(const QString&)' member function declared in class 'byteConverterDialog'
    18. ..\byteConverter\byteconverterdialog.cpp:98: error: no 'void byteConverterDialog::binChanged(const QString&)' member function declared in class 'byteConverterDialog'
    19. mingw32-make[1]: *** [debug/byteconverterdialog.o] Error 1
    20. mingw32-make: *** [debug] Error 2
    21. The process "C:/Qt/2010.04/mingw/bin/mingw32-make.exe" exited with code %2.
    22. Error while building project byteConverter (target: Desktop)
    23. When executing build step 'Make'
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: need help in qt code

    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...

  6. #6
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: need help in qt code

    To answer your question of where to add it:

    Qt Code:
    1. #ifndef BYTECONVERTERDIALOG_H
    2. #define BYTECONVERTERDIALOG_H
    3.  
    4. #include <QDialog>
    5.  
    6. class byteConverterDialog : public QDialog
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. byteConverterDialog();
    12.  
    13. private:
    14. // <----------Here.
    15. };
    16.  
    17. #endif // BYTECONVERTERDIALOG_H
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. help! how to code this gui with qt4?
    By khong_fly2008 in forum Qt Programming
    Replies: 4
    Last Post: 6th April 2009, 14:39
  2. Pasting code from code tag in emacs
    By Gopala Krishna in forum General Discussion
    Replies: 0
    Last Post: 16th February 2007, 05:47
  3. use of VC++ code in Qt
    By shailesh in forum Qt Programming
    Replies: 4
    Last Post: 28th April 2006, 11:11
  4. Need help with C++ code!
    By therealjag in forum Qt Programming
    Replies: 4
    Last Post: 20th March 2006, 21:37
  5. C++ code
    By therealjag in forum Newbie
    Replies: 8
    Last Post: 14th March 2006, 19:48

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.