PDA

View Full Version : another Q_OBJECT and pointers (?)



caminoix
26th March 2006, 01:07
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:

In file included from moc_mainwindow.cpp:11:
mainwindow.h: In constructor `mainWindow::mainWindow(QWidget*, char*, guiClient*)':
mainwindow.h:26: error: `socketSend' undeclared (first use this function)
mainwindow.h:26: error: (Each undeclared identifier is reported only once for each function it appears in.)
make: *** [moc_mainwindow.o] Błąd 1
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...

jacek
26th March 2006, 01:12
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?

caminoix
26th March 2006, 07:41
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:

#include <qapplication.h>

#include "guiclient.h"
#include "mainwindow.h"

int main(int argc,char **argv){
guiClient *socket=new guiClient("127.0.0.1",13000);
QApplication loko(argc,argv);
mainWindow *mainWin=new mainWindow(0,0,socket);
loko.setMainWidget(mainWin);
mainWin->show();
return loko.exec();
}
guiclient.h:

#ifndef GUICLIENT_H
#define GUICLIENT_H

#include <qsocket.h>

class guiClient:public QSocket{
public:
guiClient(const QString &host,Q_UINT16 port);
void socketSend(QString question);
};

guiClient::guiClient(const QString &host,Q_UINT16 port):QSocket(){
connectToHost(host,port);
}

void guiClient::socketSend(QString question){
QTextStream os(this);
os << question << "|"; //"|" required for cooperation with the server (no "readline" in .net)
}

#endif
mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <qmainwindow.h>

class guiClient;

class mainWindow:public QMainWindow{
Q_OBJECT

public:
mainWindow(QWidget *parent=0,char *name=0,guiClient *sock=0);

private:
guiClient *s;
};

mainWindow::mainWindow(QWidget *parent,char *name,guiClient *sock):QMainWindow(parent,name){
sock->socketSend("test");
}

#endif

guestgulkan
26th March 2006, 11:37
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

caminoix
26th March 2006, 20:18
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?

jacek
26th March 2006, 20:25
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.

caminoix
26th March 2006, 20:35
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?

jacek
26th March 2006, 21:00
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.

caminoix
26th March 2006, 21:06
oh, ok. i think i sort of begin to get it. thanks :)