PDA

View Full Version : trouble linking to public slot



jiggersplat
22nd February 2012, 23:08
I am having trouble calling a slot directly from subclass of QListWidget. It fails to link claiming an undefined reference to MyClass::MySlot. If instead of calling it, I just connect to slot and emit the appropriate signal everything works fine. It seems to me that it's probably a compiler/qmake/makefile issue but I'm not sure. Below is a rough approximation of the issue I'm having. I have omitted all the #include for the sake of brevity.

I have a subclass of QObject that looks roughly like



class MyClass : public QObject {

Q_OBJECT

public:
MyClass(QObject* parent = NULL) : QObject(parent) { }
void MyFunc() {
// do some work
}

public slots:
void MySlot() {
// do some work
}
}


And...



class MyListWidget : public QListWidget {

Q_OBJECT

private:
MyClass* foo;

public:
MyListWidget(QWidget* parent = NULL) : QListWidget(parent) { }

public slots:
void bar() {
// won't compile, linker fails - undefined reference to 'MyClass::MySlot'
foo->MySlot();
}

void baz() {
// this compiles and works fine
QObject::connect(this, SIGNAL(callSlot), foo, SLOT(MySlot()));
emit callSlot();
QObject::disconnect(this, SIGNAL(callSlot), foo, SLOT(MySlot()));

// also works
foo->MyFunc();
}

signals:
void callSlot();
}

mentalmushroom
23rd February 2012, 11:51
could you provide a more complete sample. it is not clear what you include and where.

jiggersplat
23rd February 2012, 15:03
MyListWidget does in fact include the header for MyClass if that's what you were getting at. Besides, it's a linker problem, not a compiler problem so I don't think #includes are the issue. I can provide more detail if you still think it will help.

mentalmushroom
23rd February 2012, 15:31
check out this code. it compiles fine and gives linker error. and of course, there is an error in the code



#include <QtCore>

class A
{
public:
void testMethod();
};

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

A a;
a.testMethod();

return app.exec();
}

jiggersplat
23rd February 2012, 15:53
I'm not sure I understand what point you are making. Your example won't link because you have not written the implementation of testMethod() anywhere, correct? The function that refuses won't link in mine has a body so that's not the problem. I can even run the function in question using signals, I just won't link when I try and call it directly.

Added after 10 minutes:

fixed it. problem with the .pro file.