What is wrong with this simple example ?
It should work (I think)
:confused:
Code:
#include <QApplication>
#include <QFont>
#include <QPushButton>
#include <QWidget>
#include <QCalendarWidget>
#include <QVBoxLayout>
#include <iostream>
{
Q_OBJECT
public:
public slots:
void calClicked
( const QDate & ) ;
} ;
void MyWidget
::calClicked ( const QDate & theDate
) { std::cout << theDate.toString( "ddd MMMM d yyyy" ).toStdString() << std::endl ;
}
{
quit->setGeometry ( 62, 40, 75, 30 ) ;
quit
->setFont
( QFont( "Times",
18,
QFont::Bold ) ) ;
connect ( quit, SIGNAL( clicked() ), qApp, SLOT(quit()) ) ;
connect ( dateEdit, SIGNAL( clicked(const QDate&) ), this, SLOT(calClicked(const QDate&)) ) ;
layout->addWidget(dateEdit);
layout->addWidget(quit);
setLayout(layout);
}
int main(int argc, char* argv[] )
{
MyWidget widget ;
widget.show() ;
return app.exec() ;
}
Here's the compile:
Quote:
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:
Quote:
Object::connect: No such slot QWidget::calClicked(QDate)
Help ! :crying:
Re: What is wrong with this simple example ?
Try putting the slot after constructor is defined.
Might help
Re: What is wrong with this simple example ?
Quote:
Originally Posted by
vermarajeev
Try putting the slot after constructor is defined.
I did. :eek:
It does not. :mad:
:confused:
:crying:
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"
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 :
Quote:
--------------------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 :( :confused:
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.
Re: What is wrong with this simple example ?
hmm...it works !!
Indeed a simple problem !! :D
Re: What is wrong with this simple example ?
Quote:
Originally Posted by
aamer4yu
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
Code:
#include <QApplication>
#include <QFont>
#include <QPushButton>
#include <QWidget>
#include <QCalendarWidget>
#include <QVBoxLayout>
#include <iostream>
{
Q_OBJECT
public:
public slots:
void calClicked
( const QDate & ) ;
} ;
void MyWidget
::calClicked ( const QDate & theDate
) { std::cout << theDate.toString( "ddd MMMM d yyyy" ).toStdString() << std::endl ;
}
{
quit->setGeometry ( 62, 40, 75, 30 ) ;
quit
->setFont
( QFont( "Times",
18,
QFont::Bold ) ) ;
connect ( quit, SIGNAL( clicked() ), qApp, SLOT(quit()) ) ;
connect ( dateEdit, SIGNAL( clicked(const QDate&) ), this, SLOT(calClicked(const QDate&)) ) ;
layout->addWidget(dateEdit);
layout->addWidget(quit);
setLayout(layout);
}
int main(int argc, char* argv[] )
{
MyWidget widget ;
widget.show() ;
return app.exec() ;
}
#include "main.moc"
Try this code out It should compile
Re: What is wrong with this simple example ?
ok I am not thinking much now.. :D
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? ?
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)
Re: What is wrong with this simple example ?