PDA

View Full Version : function 'connect' always look super class for slots.



Intaek Lim
12th March 2007, 06:14
Hello, I'm a newbie to qt programming.
These days, I write a simple image viewer with qt4 on WinXP. My code mainly consists of MyWindow derived from QMainWindow, MyImageWidget derived from QWidget.

Creating objects and showing images are all good but there's a problem with SIGNAL-SLOT connection.

Menus and actions are made in MyWindow::createMenu() before that is shown.


QMenu *fileMenu = menuBar()->addMenu("File");
openAction = fileMenu->addAction("Open");
connect(openAction, SIGNAL(triggered()), this, SLOT(fileOpen()));
return true;


Of course, openAction is a member variable of MyWindow and fileOpen() is a member function of the same class.

When I run the compiled binary, everything seem to be good, but the menu does not work and there's a error message in console window like this:

"Object::connect: No such slot QMainWindow::fileOpen()""

In my opinion, Object::connect function tries to find 'fileOpen' function in QMainWindow. But that is defined in the child class!!

I took some alternatives but I could not find any solution.


Thanks in advance.


Intaek.

jpn
12th March 2007, 06:23
Hi, add Q_OBJECT (http://doc.trolltech.com/4.2/qobject.html#Q_OBJECT) macro to the declaration of the class and re-run qmake to re-generate required steps for moc in the makefile.

Intaek Lim
12th March 2007, 06:34
Thanks, but I tried that. But when I add that macro, there are some link errors. What library should I add?



error LNK2001: "public: virtual struct QMetaObject const * __thiscall TVMainWindow::metaObject(void)const " (?metaObject@TVMainWindow@@UBEPBUQMetaObject@@XZ) tvmainwindow.obj
error LNK2001: "public: virtual void * __thiscall TVMainWindow::qt_metacast(char const *)" (?qt_metacast@TVMainWindow@@UAEPAXPBD@Z) tvmainwindow.obj
error LNK2001: "public: virtual int __thiscall TVMainWindow::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@TVMainWindow@@UAEHW4Call@QMetaObject @@HPAPAX@Z) 외부 기호를 tvmainwindow.obj

jpn
12th March 2007, 06:44
But when I add that macro, there are some link errors. What library should I add?
That's why I suggested re-running qmake. The required moc steps are missing. For signals and slots, an auto-generated piece of code must be compiled and linked. Adding the Q_OBJECT macro and re-running qmake takes care of this.

But, I see you are using Visual Studio. Do you have a commercial license and the Visual Studio integration? I think the commercial integration should take care of detecting an added Q_OBJECT macro automatically, but if you are using an open source version and the community patches, you might need to regenerate the Visual Studio project file or something.

Intaek Lim
12th March 2007, 07:07
Thank for your reply, but I don't know to qmake in WinXP environ. with VS2005.

vermarajeev
12th March 2007, 07:29
Thank for your reply, but I don't know to qmake in WinXP environ. with VS2005.

Create a seperate *.h and *.cpp file. Put your declarations in *.h and definations in *.cpp. Use Q_OBJECT macro defore defining your constructor in *.h file and before public specifier. Hit the "moc" button provided with vs-integration(which you can see at top after you have installed qt properly).

This should solve the problem.

macbeth
12th March 2007, 10:22
I'm not really sure, if I know what the problem exactly is, but some suggestions:

You should put Q_OBJECT macro inside your class and re-run qmake, but one other question crossed my mind: Did you put your function in 'public/private slots' section of the class?

Antony
12th March 2007, 13:38
if your MyWindow has more than one parent make sure that QMainWindow listed first:

class MyWindow: public QMainWindow,public AnyOtherParents

otherwise moc will parse it incorrectly.