【问题标题】:Qt and C++ - undefined reference to slotQt 和 C++ - 未定义的插槽引用
【发布时间】:2010-11-11 03:22:38
【问题描述】:

我在 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 退出状态

请帮帮我(:

【问题讨论】:

  • 这似乎真的没有足够的信息。如果您注释掉您的连接语句,错误甚至会消失吗?

标签: c++ qt signals-slots undefined-reference


【解决方案1】:

我在 Qt 构建中遇到了同样的错误。

我正在为来自 Qprocess 的信号 finished 添加一个插槽 - 文档上写着:

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)”的引用

解决方法是在我的 slotPreviewFinished 实现前加上 FreeSound::

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

【讨论】:

    【解决方案2】:

    关于语法的快速说明:通常你会使用任何一个

    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()));
    

    只是混淆和重复代码。

    【讨论】:

    • ...并引发编译器警告,因为 5-arg QObject::connect() 是静态方法...
    【解决方案3】:

    void doSomething();看起来像头文件中的一个片段,您是否实现了插槽本身?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-08
      • 2014-01-13
      • 1970-01-01
      • 2011-08-16
      • 2013-11-12
      • 1970-01-01
      相关资源
      最近更新 更多