简体   繁体   English

Qt信号插槽:发送信号但不调用插槽

[英]Qt Signal Slot: Signal is sent but Slot is not called

I'm using Qt in Visual Studio 2013 in C++. 我在C ++中使用Visual Studio 2013中的Qt。 I'm trying to connect a signal to a slot. 我正在尝试将信号连接到插槽。 The problem is that the signal is sent but the slot function is never called and I don't know what happened. 问题是信号被发送但是槽功能从未被调用过,我不知道发生了什么。 This code was working earlier but after I migrated the code from Visual Studio 2012 in 32 bit to Visual Studio 2013 in 64 bit and made some changes, it doesn't work anymore. 这段代码工作得比较早,但在我将代码从32位的Visual Studio 2012迁移到64位的Visual Studio 2013并进行了一些更改之后,它就不再起作用了。 It prints the debug statements: before sending, image sent, and connected but it doesn't print out image received. 它打印调试语句:在发送,图像发送和连接之前,但它不打印出接收的图像。 Can someone please help? 有人可以帮忙吗?

Streamer.h Streamer.h

signals:
    //Signal to output frame to be displayed
    void processedImageStream(const vector<QImage> &imgs, const QImage &image2, const QImage &image3, const QImage &image4);

Streamer.cpp in the run() function run()函数中的Streamer.cpp

qDebug() << "before sending";
emit processedImageStream(imgs,imgIntel, imgIntelDepth, imgIntelIR);
qDebug() << "images sent!";

MainWindow.h MainWindow.h

private slots:
    //Display video frame in streamer UI
    void updateStreamerUI(const vector<QImage> &imgs, const QImage &imgIntel, const QImage &imgIntelDepth, const QImage &imgIntelIR);
private:
    Streamer* myStreamer;

MainWindow.cpp MainWindow.cpp

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
    //Streamer initialization
    myStreamer = new Streamer();
    QObject::connect(myStreamer, SIGNAL(processedImageStream(vector<QImage>, QImage, QImage, QImage)), this, SLOT(updateStreamerUI(vector<QImage>, QImage, QImage, QImage)));
    qDebug() << "connected";
    ui.setupUi(this);
}

//slot for when new images are sent from the Streamer class
void MainWindow::updateStreamerUI(const vector<QImage> &imgs, const QImage &imgIntel, const QImage &imgIntelDepth, const QImage &imgIntelIR) {
    qDebug() << "images received!";
    //rest of the code
}

I don't know if this is at the heart of your problem or not, but QVector already is ready for passing around in the Qt Meta Object system. 我不知道这是否是您问题的核心,但QVector已经准备好在Qt元对象系统中传递。 Custom types need to be registered. 需要注册自定义类型。

qRegisterMetaType<T>("T");

Standard vector , and standard library collections may be exempt... I haven't used them lately. 标准vector和标准库集合可以免除......我最近没有使用它们。

Qt's runtime connection of signals and slots prints out a warning to the application output when it can't perform the connection. Qt的信号和插槽的运行时连接在无法执行连接时会向应用程序输出发出警告。 You can also look at the return value from QObject::connect . 您还可以查看QObject::connect的返回值。

A side note, not directly related to the question, but I've had issues with QVector and storing local objects in it. 一个旁注,与问题没有直接关系,但我遇到了QVector问题并在其中存储了本地对象。 If done improperly, the objects will go out of scope, and you will get weird errors when accessing it. 如果操作不正确,对象将超出范围,访问时会出现奇怪的错误。 Sometimes it will not happen until you run it in debug mode. 有时,只有在调试模式下运行它才会发生。 If you initialize your objects on the heap, you don't have to worry about parts of it going out of scope, but you do need to clean up before calling clear on your collection. 如果在堆上初始化对象,则不必担心它的一部分超出范围,但是在对集合调用clear之前需要清理。

Read up on QVector and give it a spin. 阅读QVector并给它一个旋转。

Also at the top of each of your slots, put the following: 同样位于每个广告位的顶部,请输入以下内容:

qDebug() << Q_FUNC_INFO;

Signal/Slot signatures must match EXACTLY. 信号/插槽签名必须完全匹配。 I have even had problems with using directives (eg using std::string; ) with Qt in that it doesn't recognize a string as a std::string . 我甚至在using Qt的指令(例如using std::string; )时遇到了问题,因为它不能将string识别为std::string

It also looks like the declaration for MainWindow::updateStreamerUI doesn't match the definition in the .cpp file. 它看起来像MainWindow::updateStreamerUI的声明与.cpp文件中的定义不匹配。

For example, change the declaration of MainWindow::updateStreamerUI to this: 例如,将MainWindow::updateStreamerUI的声明更改为:

void updateStreamerUI(const vector<QImage> &imgs, const QImage &imgIntel, const QImage &imgIntelDepth, const QImage &imgIntelIR)

Then update the definition accordingly in MainWindow.cpp. 然后在MainWindow.cpp中相应地更新定义。

One more thing to try is to fully-qualify std::vector in both the signal AND the slot definitions AND declarations, eg 还有一件事要在信号和槽定义AND声明中完全限定std :: vector,例如

void processedImageStream(const std::vector<QImage> &imgs, const QImage &image2, const QImage &image3, const QImage &image4);

void updateStreamerUI(const std::vector<QImage> &imgs, const QImage &imgIntel, const QImage &imgIntelDepth, const QImage &imgIntelIR);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM