Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QObject question please, it's simple but I can't get it!!
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 4.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    maherali
    wrote on last edited by
    #1

    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

    life is just lines of code

    1 Reply Last reply
    0
    • R Offline
      R Offline
      renato.filho
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • M Offline
        M Offline
        maherali
        wrote on last edited by
        #3

        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

        life is just lines of code

        1 Reply Last reply
        0
        • A Offline
          A Offline
          anselmolsm
          wrote on last edited by
          #4

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

          after the class declaration.

          Anselmo L. S. Melo (anselmolsm)

          1 Reply Last reply
          0
          • M Offline
            M Offline
            maherali
            wrote on last edited by
            #5

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

            life is just lines of code

            1 Reply Last reply
            0
            • A Offline
              A Offline
              anselmolsm
              wrote on last edited by
              #6

              [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.

              Anselmo L. S. Melo (anselmolsm)

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved