Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt and C++ - undefined reference to slot

I have a build error with a slot in Qt. I have an class which has a public slot:

void doSomething();

In constructor of this class i do:

this->connect( ui->textFrom, SIGNAL(returnPressed()),
               this, SLOT(doSomething()) );

I have QLineEdit - textFrom object. The build error is

../moc_mainwindow.cpp:66: undefined reference to `MainWindow::doSomething()'

:-1: error: collect2: ld returned 1 exit status

Help me, please (:

like image 205
Max Frai Avatar asked Jul 15 '09 19:07

Max Frai


2 Answers

void doSomething(); looks like a snip from the header file, did you implement the slot itself?

like image 122
Wojciech Bederski Avatar answered Oct 14 '22 19:10

Wojciech Bederski


quick note about syntax: Usually you would use either

connect(from, SIGNAL(sig()), to, SLOT(slot()));

which is basically equivalent to

QObject::connect(from, SIGNAL(sig()), to, SLOT(slot()));

Which you'll do if you're calling from somewhere not inside a QObject.
While this syntax:

to->connect(from, SIGNAL(sig()), SLOT(slot()));

is also reasonable. But this syntax:

to->connect(from, SIGNAL(sig()), to, SLOT(slot()));

is just confusing and duplicates code.

like image 31
shoosh Avatar answered Oct 14 '22 19:10

shoosh