Results 1 to 11 of 11

Thread: What is wrong with this simple example ?

  1. #1
    Join Date
    Jan 2007
    Posts
    30
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    MacOS X

    Question What is wrong with this simple example ?

    It should work (I think)



    Qt Code:
    1. #include <QApplication>
    2. #include <QFont>
    3. #include <QPushButton>
    4. #include <QWidget>
    5. #include <QCalendarWidget>
    6. #include <QVBoxLayout>
    7. #include <iostream>
    8.  
    9. class MyWidget : public QWidget
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. MyWidget(QWidget *parent = 0) ;
    15.  
    16. public slots:
    17. void calClicked ( const QDate & ) ;
    18. } ;
    19.  
    20. void MyWidget::calClicked ( const QDate & theDate )
    21. { std::cout << theDate.toString( "ddd MMMM d yyyy" ).toStdString() << std::endl ;
    22. }
    23.  
    24. MyWidget::MyWidget ( QWidget *parent ) : QWidget ( parent )
    25. {
    26. QPushButton *quit = new QPushButton ( tr ( "Quit" ), this ) ;
    27. quit->setGeometry ( 62, 40, 75, 30 ) ;
    28. quit->setFont ( QFont( "Times", 18, QFont::Bold ) ) ;
    29. connect ( quit, SIGNAL( clicked() ), qApp, SLOT(quit()) ) ;
    30.  
    31. QCalendarWidget *dateEdit = new QCalendarWidget () ;
    32. connect ( dateEdit, SIGNAL( clicked(const QDate&) ), this, SLOT(calClicked(const QDate&)) ) ;
    33.  
    34. QVBoxLayout *layout = new QVBoxLayout;
    35. layout->addWidget(dateEdit);
    36. layout->addWidget(quit);
    37. setLayout(layout);
    38.  
    39. }
    40.  
    41. int main(int argc, char* argv[] )
    42. {
    43. QApplication app(argc, argv ) ;
    44. MyWidget widget ;
    45. widget.show() ;
    46. return app.exec() ;
    47. }
    To copy to clipboard, switch view to plain text mode 

    Here's the compile:
    c++ -headerpad_max_install_names -o DIY-Cal.app/Contents/MacOS/DIY-Cal main.o -L/usr/local/Trolltech/Qt-4.2.2/lib -lQtGui -L/Volumes/Whopper07/TiBookPurge/qt-mac-opensource-src-4.2.2/lib -framework Carbon -framework QuickTime -framework AppKit -lQtCore -lz -lm -liconv -framework ApplicationServices
    /usr/bin/ld: Undefined symbols:
    MyWidget::staticMetaObject
    vtable for MyWidget
    collect2: ld returned 1 exit status
    make: *** [DIY-Cal.app/Contents/MacOS/DIY-Cal] Error 1
    What is even weirder, it WILL compile if I remove "Q_OBJECT", but when I run it, it says:
    Object::connect: No such slot QWidget::calClicked(QDate)
    Help !
    Last edited by jacek; 16th January 2007 at 12:23. Reason: changed [code] to [quote]

  2. #2
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: What is wrong with this simple example ?

    Try putting the slot after constructor is defined.

    Might help

  3. #3
    Join Date
    Jan 2007
    Posts
    30
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    MacOS X

    Default Re: What is wrong with this simple example ?

    Quote Originally Posted by vermarajeev View Post
    Try putting the slot after constructor is defined.
    I did.
    Might help
    It does not.



  4. #4
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: What is wrong with this simple example ?

    You seem to have forgotten to inclde the moc file generated. Suppose the file name is MyWidget.cpp. after the main function, #include "MyWidget.moc"
    We can't solve problems by using the same kind of thinking we used when we created them

  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: What is wrong with this simple example ?

    I dont think thats the case...
    and I even dont think this is a simple example...
    I tried the code too, (VC++ 6.0 )but it is giving the following errors :

    --------------------Configuration: test - Win32 Release--------------------
    Running MOC on release\test.moc
    Compiling...
    dump.cpp
    test.cpp
    Generating Code...
    Linking...
    test.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall MyWidget::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@MyWidget@@UAEHW4Call@QMetaObject@@HP APAX@Z)
    test.obj : error LNK2001: unresolved external symbol "public: virtual void * __thiscall MyWidget::qt_metacast(char const *)" (?qt_metacast@MyWidget@@UAEPAXPBD@Z)
    test.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall MyWidget::metaObject(void)const " (?metaObject@MyWidget@@UBEPBUQMetaObject@@XZ)
    test.obj : error LNK2001: unresolved external symbol "public: static struct QMetaObject const MyWidget::staticMetaObject" (?staticMetaObject@MyWidget@@2UQMetaObject@@B)
    release/test.exe : fatal error LNK1120: 4 unresolved externals
    Error executing link.exe.

    test.exe - 5 error(s), 0 warning(s)
    when i remove the slot and Q_OBJECT from the code, it works fine... thers something wrong with the slot function .... I am really confused too
    Last edited by jacek; 16th January 2007 at 12:22. Reason: changed [code] to [quote]

  6. #6
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: What is wrong with this simple example ?

    Move you class definition into a header and use qmake.
    Q_OBJECT in a class defintion inside a source file does not work.

  7. The following user says thank you to ChristianEhrlicher for this useful post:

    aamer4yu (16th January 2007)

  8. #7
    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: What is wrong with this simple example ?

    hmm...it works !!

    Indeed a simple problem !!

  9. #8
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: What is wrong with this simple example ?

    Quote Originally Posted by aamer4yu View Post
    I dont think thats the case...
    and I even dont think this is a simple example...
    I tried the code too, (VC++ 6.0 )but it is giving the following errors :
    I think you shouldn't think too much, and just try out the solution mentioned

    Qt Code:
    1. #include <QApplication>
    2. #include <QFont>
    3. #include <QPushButton>
    4. #include <QWidget>
    5. #include <QCalendarWidget>
    6. #include <QVBoxLayout>
    7. #include <iostream>
    8.  
    9. class MyWidget : public QWidget
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. MyWidget(QWidget *parent = 0) ;
    15.  
    16. public slots:
    17. void calClicked ( const QDate & ) ;
    18. } ;
    19.  
    20. void MyWidget::calClicked ( const QDate & theDate )
    21. { std::cout << theDate.toString( "ddd MMMM d yyyy" ).toStdString() << std::endl ;
    22. }
    23.  
    24. MyWidget::MyWidget ( QWidget *parent ) : QWidget ( parent )
    25. {
    26. QPushButton *quit = new QPushButton ( tr ( "Quit" ), this ) ;
    27. quit->setGeometry ( 62, 40, 75, 30 ) ;
    28. quit->setFont ( QFont( "Times", 18, QFont::Bold ) ) ;
    29. connect ( quit, SIGNAL( clicked() ), qApp, SLOT(quit()) ) ;
    30.  
    31. QCalendarWidget *dateEdit = new QCalendarWidget () ;
    32. connect ( dateEdit, SIGNAL( clicked(const QDate&) ), this, SLOT(calClicked(const QDate&)) ) ;
    33.  
    34. QVBoxLayout *layout = new QVBoxLayout;
    35. layout->addWidget(dateEdit);
    36. layout->addWidget(quit);
    37. setLayout(layout);
    38.  
    39. }
    40.  
    41. int main(int argc, char* argv[] )
    42. {
    43. QApplication app(argc, argv ) ;
    44. MyWidget widget ;
    45. widget.show() ;
    46. return app.exec() ;
    47. }
    48.  
    49. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 


    Try this code out It should compile
    We can't solve problems by using the same kind of thinking we used when we created them

  10. #9
    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: What is wrong with this simple example ?

    ok I am not thinking much now..
    can u tell more ? how does moc work ?
    if main.moc was not included, main_moc.cpp might have been generated ? hope I am right... are there any other files generated ??

    how do one choose to what to include? ?

  11. #10
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: What is wrong with this simple example ?

    As you can moc can work on a source and on a header file. For a beginner it's imo better to just use the header-file approach.
    moc on src file creates a <filename>.moc which has to be included in <filename>.cpp
    moc on header file creates moc_<filename>.cpp which is automagically added to the sourcefile-list by qmake.

    Be aware that cmake & automake use a different naming sheme (and you have to include moc src and moc header - it's not done by the buildsystem there)

  12. #11
    Join Date
    Jan 2007
    Posts
    30
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    MacOS X

    Default Re: What is wrong with this simple example ?

    Thanks. That did it .

Similar Threads

  1. Simple way to expand all nodes on a QTreeView?
    By cboles in forum Qt Programming
    Replies: 10
    Last Post: 12th April 2014, 17:54
  2. QTextEdit simple question
    By Marcopolo in forum Qt Tools
    Replies: 4
    Last Post: 11th October 2007, 01:01
  3. Release my simple program to other users ?
    By probine in forum Qt Programming
    Replies: 9
    Last Post: 10th July 2006, 00:42
  4. QListWidget...what's wrong
    By nupul in forum Newbie
    Replies: 16
    Last Post: 4th April 2006, 13:17
  5. can't get a simple dialog working
    By pthomas in forum Newbie
    Replies: 8
    Last Post: 13th January 2006, 15: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.