[英]Qt and C++ - undefined reference to slot
我在Qt中有一個插槽的構建錯誤。 我有一個有公共插槽的課程:
void doSomething();
在這個類的構造函數中我做:
this->connect( ui->textFrom, SIGNAL(returnPressed()),
this, SLOT(doSomething()) );
我有QLineEdit - textFrom對象。 構建錯誤是
../moc_mainwindow.cpp:66: undefined reference to `MainWindow::doSomething()'
:-1:錯誤:collect2:ld返回1退出狀態
請幫幫我 (:
void doSomething(); 看起來像頭文件中的剪輯,你是否實現了插槽本身?
關於語法的快速說明:通常你會使用其中之一
connect(from, SIGNAL(sig()), to, SLOT(slot()));
這基本上相當於
QObject::connect(from, SIGNAL(sig()), to, SLOT(slot()));
如果您從不在QObject內部的某個地方打電話,那么您將會這樣做。
雖然這個語法:
to->connect(from, SIGNAL(sig()), SLOT(slot()));
也是合理的。 但是這個語法:
to->connect(from, SIGNAL(sig()), to, SLOT(slot()));
只是混淆和復制代碼。
我在Qt版本中遇到了同樣的錯誤。
我正在為Qprocess 完成的信號添加一個插槽 - doco上面寫着:
void QProcess::finished(int exitCode, QProcess::ExitStatus exitStatus)
我的代碼:
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;
}
編譯上面生成的:/home/ttguy/kdenlive/kdenlive_git/build-kdenlive-Desktop-Default/src/moc_freesound.cpp:121:錯誤:未定義引用`FreeSound :: slotPreviewFinished(int,QProcess :: ExitStatus)'
修復是使用FreeSound ::為我的slotPreviewFinished實現添加前綴
void FreeSound::slotPreviewFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
qDebug()<<"// slotPreviewFinished: "<<exitCode;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.