Results 1 to 7 of 7

Thread: Why this error?

  1. #1
    Join Date
    Jan 2010
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Why this error?

    Hi,
    I'm trying to compile this code
    Qt Code:
    1. #include <QApplication>
    2. #include <QLCDNumber>
    3. #include <QPushButton>
    4. #include <QVBoxLayout>
    5. #include <QWidget>
    6.  
    7. class LCDCounter : public QLCDNumber{
    8. Q_OBJECT
    9.  
    10. private:
    11. int counter;
    12. public:
    13. LCDCounter(QWidget *parent = 0);
    14. public slots:
    15. void increase(void);
    16.  
    17. };
    18.  
    19. LCDCounter::LCDCounter(QWidget *parent)
    20. : QLCDNumber(parent)
    21. {
    22. counter = 0;
    23. setSegmentStyle(QLCDNumber::Filled);
    24. display(0);
    25. }
    26.  
    27. void LCDCounter::increase(){
    28. display(counter++);
    29. }
    30.  
    31. class MyWidget : public QWidget
    32. {
    33. QPushButton *clickMe;
    34. LCDCounter *counter;
    35.  
    36. public:
    37. MyWidget(QWidget *parent = 0);
    38. };
    39.  
    40. MyWidget::MyWidget(QWidget *parent)
    41. : QWidget(parent)
    42. {
    43. clickMe = new QPushButton("Click me!");
    44. counter = new LCDCounter();
    45. connect(clickMe,SIGNAL(clicked()),counter,SLOT(increase()));
    46.  
    47. QVBoxLayout *boxLayout = new QVBoxLayout;
    48. boxLayout->addWidget(clickMe);
    49. boxLayout->addWidget(counter);
    50. setLayout(boxLayout);
    51.  
    52. }
    53.  
    54. int main(int argc, char *argv[])
    55. {
    56. QApplication a(argc, argv);
    57. MyWidget w;
    58. w.show();
    59. return a.exec();
    60. }
    To copy to clipboard, switch view to plain text mode 

    But this error is returned.
    Qt Code:
    1. 1>main.obj : error LNK2001: sÃ*mbolo externo "public: virtual struct QMetaObject const * __thiscall LCDCounter::metaObject(void)const " (?metaObject@LCDCounter@@UBEPBUQMetaObject@@XZ) unresolved
    2. 1>main.obj : error LNK2001: sÃ*mbolo externo "public: virtual void * __thiscall LCDCounter::qt_metacast(char const *)" (?qt_metacast@LCDCounter@@UAEPAXPBD@Z) unresolved
    3. 1>main.obj : error LNK2001: sÃ*mbolo externo "public: virtual int __thiscall LCDCounter::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@LCDCounter@@UAEHW4Call@QMetaObject@@HPAPAX@Z) unresolved
    4. 1>C:\Users\ferran\directo\QT_Tutorials\Debug\Tutorial02.exe : fatal error LNK1120: 3 externos sin resolver
    To copy to clipboard, switch view to plain text mode 

    How do I guess the method that fails? What are happening?

    Many thanks in advance.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Why this error?

    QLCDNumber is already a QObject so you don't require the Q_OBJECT macro in your derived class definition. The linker is getting confused looking in the wrong place for the tables used to support QObjects.

  3. #3
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Why this error?

    but if he removes Q_OBJECT the connect will be failed ... at console he will receive
    Object::connect: No such slot QLCDNumber::increase()
    "Behind every great fortune lies a crime" - Balzac

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Why this error?

    MOC isn't being run. If your using the QtCreator IDE, then use Build->Run QMake. Leave the Q_OBJECT in there, it's required.

  5. #5
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Why this error?

    but in normal qt compilation ...

    he should include "main.moc" in his main file

    code will be

    Qt Code:
    1. #include <QApplication>
    2. #include <QtGui>
    3.  
    4. class LCDCounter : public QLCDNumber{
    5. Q_OBJECT
    6.  
    7. private:
    8. int counter;
    9. public:
    10. LCDCounter(QWidget *parent = 0)
    11. :QLCDNumber(parent)
    12. {
    13. counter = 0;
    14. setSegmentStyle(QLCDNumber::Filled);
    15. display(0);
    16. }
    17. public slots:
    18. void increase()
    19. {
    20. display(counter++);
    21.  
    22. }
    23.  
    24. };
    25. class MyWidget : public QWidget
    26. {
    27. Q_OBJECT
    28. public:
    29. MyWidget()
    30. : QWidget(0)
    31. {
    32. clickMe = new QPushButton("Click me!");
    33. counter = new LCDCounter();
    34. connect(clickMe,SIGNAL(clicked()),counter,SLOT(increase()));
    35. QVBoxLayout *boxLayout = new QVBoxLayout;
    36. boxLayout->addWidget(clickMe);
    37. boxLayout->addWidget(counter);
    38. setLayout(boxLayout);
    39.  
    40. }
    41.  
    42.  
    43. private:
    44. QPushButton *clickMe;
    45. LCDCounter *counter;
    46. };
    47.  
    48.  
    49. #include "main.moc"
    50.  
    51. int main(int argc, char *argv[])
    52. {
    53. QApplication a(argc, argv);
    54. MyWidget w;
    55. w.show();
    56. return a.exec();
    57. }
    To copy to clipboard, switch view to plain text mode 
    "Behind every great fortune lies a crime" - Balzac

  6. #6
    Join Date
    Jan 2010
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Why this error?

    Hi all and many thanks for your responses.

    MOC isn't being run. If your using the QtCreator IDE, then use Build->Run QMake. Leave the Q_OBJECT in there, it's required.
    I forgot to say that I'm working with Visual Studio 2008 and QT 4.6.
    With the Visual Studio add-in, the project created with the assistant doesn't includes the moc file as wagmare says. I'll try the changes you are suggesting.

    Thanks

  7. #7
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Why this error?

    include "main.moc" above main and macro Q_OBJECT in MyWidget class .. your code is working fine ...
    "Behind every great fortune lies a crime" - Balzac

  8. The following user says thank you to wagmare for this useful post:

    killrazor (18th January 2010)

Similar Threads

  1. no compile error, but error on run with QMenu QAction
    By sincnarf in forum Qt Programming
    Replies: 4
    Last Post: 4th May 2011, 11:05
  2. Replies: 0
    Last Post: 4th November 2009, 10:21
  3. Replies: 1
    Last Post: 25th October 2008, 19:18
  4. qTextEdit error - clipboard error
    By bruccutler in forum Qt Programming
    Replies: 1
    Last Post: 21st May 2007, 09:21
  5. ERROR:: QPainter: Internal error; no available GC
    By Krishnacins in forum Qt Programming
    Replies: 2
    Last Post: 8th March 2006, 06:05

Tags for this Thread

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.