Results 1 to 9 of 9

Thread: another Q_OBJECT and pointers (?)

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2006
    Posts
    5
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default another Q_OBJECT and pointers (?)

    hello

    i have a qt interface driven by a tcp connection with an engine in nemerle.

    for the connection, i use an instance (called socket) of my guiClient class derived from QSocket. it is created in main.cpp, where i also create mainWin, ie. an instance of my mainWindow class derived from QMainWindow. a pointer to socket is passed to mainWin.

    in mainWin, i try to use guiClient's method called socketSend.

    everything works fine until i use the Q_OBJECT macro in mainwindow.cpp.
    this is the error i keep getting:
    Qt Code:
    1. In file included from moc_mainwindow.cpp:11:
    2. mainwindow.h: In constructor `mainWindow::mainWindow(QWidget*, char*, guiClient*)':
    3. mainwindow.h:26: error: `socketSend' undeclared (first use this function)
    4. mainwindow.h:26: error: (Each undeclared identifier is reported only once for each function it appears in.)
    5. make: *** [moc_mainwindow.o] Błąd 1
    To copy to clipboard, switch view to plain text mode 
    i've tried creating the socket in mainwindow instead of main but the whole thing repeats itself whenever i try to pass it to some widget created from mainWin.

    maybe the whole idea of defining a seperate class for socket is wrong? but how else can i manage it? or maybe should i pass a reference to socket in some other way than a pointer?

    please, help me...

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: another Q_OBJECT and pointers (?)

    How did you declare that socketSend method and where exactly did you put that Q_OBJECT macro? Did you add proper #include directive for that guiClient class?
    Last edited by jacek; 26th March 2006 at 01:16.

  3. #3
    Join Date
    Mar 2006
    Posts
    5
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: another Q_OBJECT and pointers (?)

    well, i certainly must have done something improper since the whole thing doesn't want to compile. the problem is that i have no idea where exactly my fault may be.

    main.cpp:
    Qt Code:
    1. #include <qapplication.h>
    2.  
    3. #include "guiclient.h"
    4. #include "mainwindow.h"
    5.  
    6. int main(int argc,char **argv){
    7. guiClient *socket=new guiClient("127.0.0.1",13000);
    8. QApplication loko(argc,argv);
    9. mainWindow *mainWin=new mainWindow(0,0,socket);
    10. loko.setMainWidget(mainWin);
    11. mainWin->show();
    12. return loko.exec();
    13. }
    To copy to clipboard, switch view to plain text mode 
    guiclient.h:
    Qt Code:
    1. #ifndef GUICLIENT_H
    2. #define GUICLIENT_H
    3.  
    4. #include <qsocket.h>
    5.  
    6. class guiClient:public QSocket{
    7. public:
    8. guiClient(const QString &host,Q_UINT16 port);
    9. void socketSend(QString question);
    10. };
    11.  
    12. guiClient::guiClient(const QString &host,Q_UINT16 port):QSocket(){
    13. connectToHost(host,port);
    14. }
    15.  
    16. void guiClient::socketSend(QString question){
    17. QTextStream os(this);
    18. os << question << "|"; //"|" required for cooperation with the server (no "readline" in .net)
    19. }
    20.  
    21. #endif
    To copy to clipboard, switch view to plain text mode 
    mainwindow.h:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <qmainwindow.h>
    5.  
    6. class guiClient;
    7.  
    8. class mainWindow:public QMainWindow{
    9. Q_OBJECT
    10.  
    11. public:
    12. mainWindow(QWidget *parent=0,char *name=0,guiClient *sock=0);
    13.  
    14. private:
    15. guiClient *s;
    16. };
    17.  
    18. mainWindow::mainWindow(QWidget *parent,char *name,guiClient *sock):QMainWindow(parent,name){
    19. sock->socketSend("test");
    20. }
    21.  
    22. #endif
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Gloucester, UK
    Posts
    18
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: another Q_OBJECT and pointers (?)

    You have two seperate compiliatin problems here.

    1. As jacek described, you have not properly declared socketSend correctly/in the correct place

    2. Another issue is the way you have designed your files. They contain both the definition of the class AND the implementation of the class.
    This creates a problem under QT(3) when you use the Q_OBJECT macro in a class
    You will probably see a lot of errors about certain function being declarred/defined more than once.

    You need to split out the header files and create seperate H and CPP files for each class

  5. The following user says thank you to guestgulkan for this useful post:

    caminoix (26th March 2006)

  6. #5
    Join Date
    Mar 2006
    Posts
    5
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: another Q_OBJECT and pointers (?)

    i don't really understand what you actually mean by 1., but 2. turned out to be enough. thanks
    so, in other words, i need two separate files for each class i'm using? is it a rule or can there be exceptions?

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: another Q_OBJECT and pointers (?)

    Quote Originally Posted by caminoix
    is it a rule or can there be exceptions?
    It is a rule and there are exceptions

    If you want to use your class only within one compilation unit, you don't need the header file, but if your class has only "inline" or abtract methods, you don't need the .cpp file.

  8. #7
    Join Date
    Mar 2006
    Posts
    5
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: another Q_OBJECT and pointers (?)

    heh, i see
    so if i have f.ex. a myHBox class only used in myGroupBox class, which in its turn is accessed from myMainWindow and mySocket, than i need to have everything .h and .cpp except for myHBox for which a .cpp would be enough. is this correct?

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: another Q_OBJECT and pointers (?)

    Quote Originally Posted by caminoix
    except for myHBox for which a .cpp would be enough. is this correct?
    Yes, provided that it's the same file in which myGroupBox implementation was placed.

    One more thing: If you place a class definition with Q_OBJECT macro in somefile.cpp, then you must also add #include "somefile.moc" at the end of that file.

  10. The following user says thank you to jacek for this useful post:

    caminoix (26th March 2006)

  11. #9
    Join Date
    Mar 2006
    Posts
    5
    Thanks
    2
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: another Q_OBJECT and pointers (?)

    oh, ok. i think i sort of begin to get it. thanks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.