Qt and C++ - undefined reference to slot
Asked Answered
E

3

6

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 (:

Excurvature answered 15/7, 2009 at 19:53 Comment(1)
This doesn't seem like enough information really. If you comment out your connection statement does the error even go away?Constructivism
P
16

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

Profane answered 15/7, 2009 at 20:3 Comment(0)
A
1

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.

Acidforming answered 15/7, 2009 at 20:17 Comment(1)
...and throws a compiler warning, because 5-arg QObject::connect() is a static method...Sepulture
E
0

I was getting same error in a Qt build.

I was adding a slot for the signal finished from Qprocess - the doco on which says:

void QProcess::finished(int exitCode, QProcess::ExitStatus exitStatus)

My code:

freesound.h

void slotPreviewFinished(int exitCode, QProcess::ExitStatus exitStatus);

freensound.cpp

 m_previewProcess = new(Qprocess);
 connect (m_previewProcess ,SIGNAL (finished(int , QProcess::ExitStatus )),this,SLOT(slotPreviewFinished(int , QProcess::ExitStatus)));

 void slotPreviewFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
     qDebug()<<"// slotPreviewFinished: "<<exitCode;
}

compiling the above generated: /home/ttguy/kdenlive/kdenlive_git/build-kdenlive-Desktop-Default/src/moc_freesound.cpp:121: error: undefined reference to `FreeSound::slotPreviewFinished(int, QProcess::ExitStatus)'

And the fix was to prefix my implementation of slotPreviewFinished with FreeSound::

void FreeSound::slotPreviewFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
     qDebug()<<"// slotPreviewFinished: "<<exitCode;
}
Ethylethylate answered 26/11, 2015 at 6:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.