Qt connect “no such slot” when slot definitely does exist

孤街醉人 提交于 2019-12-05 10:27:49

Check whether whether that moc_mainwindow.cpp is in your Build Path. Or you are using some other moc_window.cpp file. Because, for ex: In QtCreator, it build the source to a new build directory. And also it uses old moc_cpp file(s) if you try to open the source in a different location.

What I am trying to say is the moc file which you checked may contain those slot definition, but compiler might be using some other moc file which was created earlier.

I solved problem by adding Q_OBJECT macro in mainwindow class.

You need

connect(acnInt, SIGNAL(callback_comp_connected(QUuid)),         this, SLOT(on_comp_connected(const QUuid&))); 

Pass by value shouldn't match pass a const reference.

But I tried it and it does. I created a minimal project with QtCreator 2.4.1 using Qt 4.7.4 on Windows. I added a single label to the main window and modified MainWindow.cpp as follows

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(
      this, SIGNAL(testSendQuuid(QUuid)),
      this, SLOT(on_comp_connected(QUuid))
    );
    QUuid x = QUuid::createUuid();
    emit testSendQuuid(x);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_comp_connected(const QUuid &qcid)
{
    ui->label->setText(qcid.toString());
}

And I get a uuid on my main window.

I also tried with the two QUuid arguments in the connect changed to const QUuid& and that worked too.

So your problem must be build related.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!