July 3, 2011

maherali maherali
Lab Rat
38 posts

QObject question please, it’s simple but I can’t get it!!

 

Hi all
I have a question about writing QObject in the main.cpp file, it’s seems like a trivial task, but I can’t believe it, I can’t get it to work, and I don’t know what I miss in this trivial program.

  1. class TempClass:public QObject
  2. {
  3.     Q_OBJECT
  4. public:
  5.     explicit TempClass(QObject * parent=0);
  6. public slots:
  7.     void closed();
  8. };
  9.  
  10. void TempClass::closed()
  11. {
  12.  
  13. }
  14.  
  15. TempClass::TempClass(QObject *parent)
  16.     :QObject(parent)
  17. {
  18. }

I’ve tried to put it before the main method but it’s not working, so I’ve putted after the main method with class forward declaration but also it’s not working.

with vtable reference error.

I’ve also tried to put all methods inside the class including the signal but having no luck at all.

do I miss something in QObject implementation? although I’ve read the QObject details many times.
Or it’s a problem with QT Creator.
You can replicate this problem easily.

Thanks in Advance

 Signature 

life is just lines of code

5 replies

July 3, 2011

renato.filho renato.filho
Lab Rat
54 posts

You need generate the moc file for this object, I do not know if qmake do this when the class declaration is inside of main file. but you can do this manually (check moc command documentiton, moc —help).

July 3, 2011

maherali maherali
Lab Rat
38 posts

Ok thank you renato.filho
I thought it could be an easy task, I’m a beginner in Qt so I’ll leave it right now.

Maybe after few weeks I’ll return to this issue.

Thank you

 Signature 

life is just lines of code

July 3, 2011

anselmolsm anselmolsm
Ant Farmer
417 posts

If you declare in the main file a class that needs to be evaluated by moc, write

  1. #include "main.moc"

after the class declaration.

 Signature 

Anselmo L. S. Melo (anselmolsm)
www.anselmolsm.org

July 3, 2011

maherali maherali
Lab Rat
38 posts

error: main.moc: No such file or directory
sorry for that but the solution doesn’t work with the above message.

 Signature 

life is just lines of code

July 3, 2011

anselmolsm anselmolsm
Ant Farmer
417 posts
maherali wrote:
error: main.moc: No such file or directory

Did you run qmake again?

maherali wrote:
sorry for that but the solution doesn’t work with the above message.

It works.

  1. anselmo@skadi ~/projects/Qt/devnet/project $ ls
  2. main.cpp  project.pro

main.cpp

  1. class TempClass:public QObject
  2. {
  3.     Q_OBJECT
  4. public:
  5.     explicit TempClass(QObject * parent=0)
  6.         :QObject(parent) {}
  7. public slots:
  8.     void closed() { }
  9. };
  10.  
  11. #include "main.moc"
  12.  
  13. int main(int argc, char *argv[])
  14. {
  15.     QApplication app(argc, argv);
  16.  
  17.     TempClass temp;
  18.  
  19.     return app.exec();
  20. }

project.pro

  1. TEMPLATE = app
  2. TARGET =
  3. DEPENDPATH += .
  4. INCLUDEPATH += .
  5.  
  6. # Input
  7. SOURCES += main.cpp

  1. anselmo@skadi ~/projects/Qt/devnet/project $ qmake
  2. anselmo@skadi ~/projects/Qt/devnet/project $ make
  3. /usr/bin/moc -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/default -I. -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I. -I. main.cpp -o main.moc
  4. g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/default -I. -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I. -I. -o main.o main.cpp
  5. g++ -Wl,-O1 -o project main.o    -L/usr/lib -lQtGui -L/usr/lib -L/usr/X11R6/lib -lQtCore -lpthread
  6.  
  7. anselmo@skadi ~/projects/Qt/devnet/project $ ls
  8. main.cpp  main.moc  main.o  Makefile  project  project.pro

project is the binary. Attention to the first line of the compilation output, there you can see moc being called to generate main.moc.

 Signature 

Anselmo L. S. Melo (anselmolsm)
www.anselmolsm.org

 
  ‹‹ Help with what should i include for translation at my app.      how to debug and get the source of signal and slots linking error’s ››

You must log in to post a reply. Not a member yet? Register here!