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.

    @class TempClass:public QObject
    {
    Q_OBJECT
    public:
    explicit TempClass(QObject * parent=0);
    public slots:
    void closed();
    };

    void TempClass::closed()
    {

    }

    TempClass::TempClass(QObject *parent)
    :QObject(parent)
    {
    }@

    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



  • 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).



  • 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



  • If you declare in the main file a class that needs to be evaluated by moc, write
    @
    #include "main.moc"
    @

    after the class declaration.



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



  • [quote author="maherali" date="1309665951"]error: main.moc: No such file or directory
    [/quote]

    Did you run qmake again?

    [quote author="maherali" date="1309665951"]
    sorry for that but the solution doesn't work with the above message.
    [/quote]

    It works.

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

    main.cpp
    @
    class TempClass:public QObject
    {
    Q_OBJECT
    public:
    explicit TempClass(QObject * parent=0)
    :QObject(parent) {}
    public slots:
    void closed() { }
    };

    #include "main.moc"

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

    TempClass temp;
    
    return app.exec();
    

    }
    @

    project.pro
    @
    TEMPLATE = app
    TARGET =
    DEPENDPATH += .
    INCLUDEPATH += .

    Input

    SOURCES += main.cpp
    @

    @
    anselmo@skadi ~/projects/Qt/devnet/project $ qmake
    anselmo@skadi ~/projects/Qt/devnet/project $ make
    /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
    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
    g++ -Wl,-O1 -o project main.o -L/usr/lib -lQtGui -L/usr/lib -L/usr/X11R6/lib -lQtCore -lpthread

    anselmo@skadi ~/projects/Qt/devnet/project $ ls
    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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.