PDA

View Full Version : What is wrong with this simple example ?



igor
16th January 2007, 02:37
It should work (I think)

:confused:



#include <QApplication>
#include <QFont>
#include <QPushButton>
#include <QWidget>
#include <QCalendarWidget>
#include <QVBoxLayout>
#include <iostream>

class MyWidget : public QWidget
{
Q_OBJECT

public:
MyWidget(QWidget *parent = 0) ;

public slots:
void calClicked ( const QDate & ) ;
} ;

void MyWidget::calClicked ( const QDate & theDate )
{ std::cout << theDate.toString( "ddd MMMM d yyyy" ).toStdString() << std::endl ;
}

MyWidget::MyWidget ( QWidget *parent ) : QWidget ( parent )
{
QPushButton *quit = new QPushButton ( tr ( "Quit" ), this ) ;
quit->setGeometry ( 62, 40, 75, 30 ) ;
quit->setFont ( QFont( "Times", 18, QFont::Bold ) ) ;
connect ( quit, SIGNAL( clicked() ), qApp, SLOT(quit()) ) ;

QCalendarWidget *dateEdit = new QCalendarWidget () ;
connect ( dateEdit, SIGNAL( clicked(const QDate&) ), this, SLOT(calClicked(const QDate&)) ) ;

QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(dateEdit);
layout->addWidget(quit);
setLayout(layout);

}

int main(int argc, char* argv[] )
{
QApplication app(argc, argv ) ;
MyWidget widget ;
widget.show() ;
return app.exec() ;
}

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 ! :crying:

vermarajeev
16th January 2007, 03:09
Try putting the slot after constructor is defined.

Might help

igor
16th January 2007, 04:06
Try putting the slot after constructor is defined.
I did. :eek:

Might help
It does not. :mad:
:confused:

:crying:

sunil.thaha
16th January 2007, 05:49
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"

aamer4yu
16th January 2007, 06:34
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 :( :confused:

ChristianEhrlicher
16th January 2007, 06:40
Move you class definition into a header and use qmake.
Q_OBJECT in a class defintion inside a source file does not work.

aamer4yu
16th January 2007, 06:49
hmm...it works !!

Indeed a simple problem !! :D

sunil.thaha
16th January 2007, 06:57
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



#include <QApplication>
#include <QFont>
#include <QPushButton>
#include <QWidget>
#include <QCalendarWidget>
#include <QVBoxLayout>
#include <iostream>

class MyWidget : public QWidget
{
Q_OBJECT

public:
MyWidget(QWidget *parent = 0) ;

public slots:
void calClicked ( const QDate & ) ;
} ;

void MyWidget::calClicked ( const QDate & theDate )
{ std::cout << theDate.toString( "ddd MMMM d yyyy" ).toStdString() << std::endl ;
}

MyWidget::MyWidget ( QWidget *parent ) : QWidget ( parent )
{
QPushButton *quit = new QPushButton ( tr ( "Quit" ), this ) ;
quit->setGeometry ( 62, 40, 75, 30 ) ;
quit->setFont ( QFont( "Times", 18, QFont::Bold ) ) ;
connect ( quit, SIGNAL( clicked() ), qApp, SLOT(quit()) ) ;

QCalendarWidget *dateEdit = new QCalendarWidget () ;
connect ( dateEdit, SIGNAL( clicked(const QDate&) ), this, SLOT(calClicked(const QDate&)) ) ;

QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(dateEdit);
layout->addWidget(quit);
setLayout(layout);

}

int main(int argc, char* argv[] )
{
QApplication app(argc, argv ) ;
MyWidget widget ;
widget.show() ;
return app.exec() ;
}

#include "main.moc"




Try this code out It should compile

aamer4yu
16th January 2007, 07:50
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? ?

ChristianEhrlicher
16th January 2007, 08:19
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)

igor
16th January 2007, 11:26
Thanks. That did it . :D