Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt Signal Slot: Signal is sent but Slot is not called

I'm using Qt in Visual Studio 2013 in C++. 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. 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

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

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

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::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
}
like image 855
user3838815 Avatar asked Sep 10 '14 23:09

user3838815


People also ask

How do I connect Qt signals and slots?

There are several ways to connect signal and slots. The first is to use function pointers: connect(sender, &QObject::destroyed, this, &MyObject::objectDestroyed); There are several advantages to using QObject::connect() with function pointers.

Are Qt signals and slots thread safe?

It is generally unsafe to provide slots in your QThread subclass, unless you protect the member variables with a mutex. On the other hand, you can safely emit signals from your QThread::run() implementation, because signal emission is thread-safe.


1 Answers

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. Custom types need to be registered.

qRegisterMetaType<T>("T");

Standard vector, and standard library collections may be exempt... I haven't used them lately.

Qt's runtime connection of signals and slots prints out a warning to the application output when it can't perform the connection. You can also look at the return value from QObject::connect.

A side note, not directly related to the question, but I've had issues with QVector and storing local objects in it. 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.

Read up on QVector and give it a spin.

Also at the top of each of your slots, put the following:

qDebug() << Q_FUNC_INFO;
like image 64
phyatt Avatar answered Nov 14 '22 23:11

phyatt