Results 1 to 5 of 5

Thread: Strange Error

  1. #1
    Join Date
    Oct 2008
    Location
    Bulgaria
    Posts
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Strange Error

    Hello, I've been testing around, but I know have problem with this program -- it's giving me strange errors when Q_OBJECT is in the class:
    the code
    (i'm using Ubuntu, if it matters):
    Qt Code:
    1. #include <QApplication>
    2. #include <QPushButton>
    3. #include <QWidget>
    4. #include <QGridLayout>
    5. #include <QString>
    6. #include <QLineEdit>
    7. #include <QVBoxLayout>
    8.  
    9. char* DigitAsString(int number)
    10. {
    11. switch(number)
    12. {
    13. case 0: return "0";
    14. case 1: return "1";
    15. case 2: return "2";
    16. case 3: return "3";
    17. case 4: return "4";
    18. case 5: return "5";
    19. case 6: return "6";
    20. case 7: return "7";
    21. case 8: return "8";
    22. case 9: return "9";
    23. default: return "";
    24. }
    25. }
    26. class Calculator: public QWidget
    27. {
    28. Q_OBJECT
    29.  
    30. public:
    31.  
    32. Calculator(QWidget* parent = 0);
    33. void InitOperationButtons();
    34. void InitDigitButtons();
    35. void InitLineEdit();
    36. void LayoutButtons();
    37. //Add(double, double);
    38. //Divide(double, double);
    39.  
    40. public slots:
    41. void numberPressed()
    42. {
    43. editLine->setText(tr("1"));
    44. }
    45.  
    46.  
    47. private:
    48. enum { NUM_KEYS = 12 };
    49. QPushButton *plusButton, *minusButton, *multipleButton, *divideButton;
    50. QPushButton *numberKeys[NUM_KEYS]; //Всички цифри като пушБутони
    51. QGridLayout *buttonsLayout; //Разположението на бутоните
    52. QVBoxLayout *commonLayout;
    53. QLineEdit* editLine;
    54. };
    55. Calculator::Calculator(QWidget* parent)
    56. : QWidget(parent)
    57. {
    58.  
    59. commonLayout = new QVBoxLayout;
    60. buttonsLayout = new QGridLayout;
    61. InitOperationButtons();
    62. InitDigitButtons();
    63. InitLineEdit();
    64. //
    65. LayoutButtons();
    66. commonLayout->addWidget(editLine);
    67. commonLayout->addLayout(buttonsLayout);
    68. //
    69. connect(numberKeys[0], SIGNAL(clicked()), this, SLOT(numberPressed()));
    70. //
    71. setWindowTitle(tr("Calculator"));
    72. setLayout(commonLayout);
    73. setFixedSize(sizeHint()); //Най - добрата фиксирана позиция за елементите
    74. }
    75. void Calculator::InitOperationButtons()
    76. {
    77. plusButton = new QPushButton(tr("+"), this);
    78. minusButton = new QPushButton(tr("-"), this);
    79. divideButton = new QPushButton(tr("/"), this);
    80. multipleButton = new QPushButton(tr("*"), this);
    81. }
    82. void Calculator::InitDigitButtons()
    83. {
    84. for(int i = 0; i < NUM_KEYS; i++)
    85. {
    86. if(i == 10)
    87. {
    88. numberKeys[i] = new QPushButton(tr("."), this);
    89. continue;
    90. }
    91. if(i == 11)
    92. {
    93. numberKeys[i] = new QPushButton(tr("="), this);
    94. continue;
    95. }
    96. numberKeys[i] = new QPushButton(tr(DigitAsString(i)), this);
    97. //Текста на бутоните ще бъде както следват цифрите
    98. }
    99. }
    100. void Calculator::InitLineEdit()
    101. {
    102. editLine = new QLineEdit;
    103. editLine->setMaxLength(15); //Дължината на числото
    104. }
    105. void Calculator::LayoutButtons()
    106. {
    107. buttonsLayout -> addWidget(plusButton, 0, 0);
    108. buttonsLayout -> addWidget(minusButton, 0, 1);
    109. buttonsLayout -> addWidget(divideButton, 0, 2);
    110. buttonsLayout -> addWidget(multipleButton, 0, 3);
    111. int row = 1;
    112. int col = 0;
    113. for(int i = 0; i < NUM_KEYS; i++, col++)
    114. {
    115. if(i % 4 == 0)
    116. {
    117. col = 0;
    118. buttonsLayout -> addWidget(numberKeys[i], ++row, col);
    119. }else
    120. {
    121. buttonsLayout -> addWidget(numberKeys[i], row, col);
    122. }
    123. }
    124. }
    125.  
    126.  
    127. int main(int argc, char* argv[])
    128. {
    129. QApplication qapp(argc, argv);
    130. Calculator* calculator = new Calculator();
    131. calculator -> show();
    132. return qapp.exec();
    133. }
    To copy to clipboard, switch view to plain text mode 
    And the errors:
    Qt Code:
    1. kotg@kotg-laptop:~/programming/calculator/test$ make
    2. g++ -Wl,--no-undefined -o test test2.o -L/usr/lib -lQtGui -lQtCore -lpthread
    3. test2.o: In function `Calculator::InitDigitButtons()':
    4. test2.cpp:(.text+0x266): undefined reference to `Calculator::staticMetaObject'
    5. test2.cpp:(.text+0x2e0): undefined reference to `Calculator::staticMetaObject'
    6. test2.cpp:(.text+0x357): undefined reference to `Calculator::staticMetaObject'
    7. test2.o: In function `Calculator::InitOperationButtons()':
    8. test2.cpp:(.text+0x473): undefined reference to `Calculator::staticMetaObject'
    9. test2.cpp:(.text+0x4cb): undefined reference to `Calculator::staticMetaObject'
    10. test2.o:test2.cpp:(.text+0x523): more undefined references to `Calculator::staticMetaObject' follow
    11. test2.o: In function `Calculator::Calculator(QWidget*)':
    12. test2.cpp:(.text+0x6cb): undefined reference to `vtable for Calculator'
    13. test2.cpp:(.text+0x6d2): undefined reference to `vtable for Calculator'
    14. test2.cpp:(.text+0x7a8): undefined reference to `Calculator::staticMetaObject'
    15. test2.o: In function `Calculator::Calculator(QWidget*)':
    16. test2.cpp:(.text+0x87b): undefined reference to `vtable for Calculator'
    17. test2.cpp:(.text+0x882): undefined reference to `vtable for Calculator'
    18. test2.cpp:(.text+0x958): undefined reference to `Calculator::staticMetaObject'
    19. collect2: ld returned 1 exit status
    20. make: *** [test] Error 1
    To copy to clipboard, switch view to plain text mode 
    Last edited by keeperofthegrove; 17th October 2008 at 17:18.

  2. #2
    Join Date
    Oct 2008
    Location
    Bulgaria
    Posts
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Strange Error

    Alright, when I comment Q_OBJECT I can't use functional slots(which is right) and when I uncomment it, it shows me the same errors as above!

  3. #3
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Strange Error

    Show me your makefile.
    I'm a rebel in the S.D.G.

  4. #4
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Strange Error

    I think this will be useful
    http://doc.trolltech.com/4.4/moc.html#moc
    I'm a rebel in the S.D.G.

  5. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Strange Error

    offtop
    you don't delete widget in this code
    Qt Code:
    1. ...
    2. Calculator* calculator = new Calculator();
    3. calculator -> show();
    4. ...
    To copy to clipboard, switch view to plain text mode 

    create this widget in stack, i.e.
    Qt Code:
    1. ...
    2. Calculator c;
    3. c.show();
    4. ...
    To copy to clipboard, switch view to plain text mode 

    or set attribute for this widget Qt::WA_DeleteOnClose
    i.e.
    Qt Code:
    1. ...
    2. Calculator* calculator = new Calculator();
    3. calculator->setAttribute(Qt::WA_DeleteOnClose);
    4. calculator -> show();
    5. ...
    To copy to clipboard, switch view to plain text mode 

    or delete this widget manually
    Qt Code:
    1. ...
    2. Calculator* calculator = new Calculator();
    3. calculator -> show();
    4. ...
    5. bool res = app.exec();
    6. delete calculator;
    7. calculator = 0;
    8. return res;
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Compile 4.4.0
    By LordQt in forum Installation and Deployment
    Replies: 18
    Last Post: 29th May 2008, 13:43
  2. QPSQL driver in windows
    By brevleq in forum Installation and Deployment
    Replies: 31
    Last Post: 14th December 2007, 12:57
  3. Error compiling psql plugin
    By vieraci in forum Installation and Deployment
    Replies: 4
    Last Post: 7th October 2007, 02:49
  4. qt 4.2.2 install on aix
    By try to remember in forum Installation and Deployment
    Replies: 2
    Last Post: 28th March 2007, 12:19
  5. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 12:52

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.