Cannot connect QT signal

I am new to QT and tried to create a custom waveform that would tell me that a removable disk was inserted. This is what I did

mainwindow.h

class MainWindow { QOBJECT .. .. signals: void qm_diskInserted(QString &); public slots: void addItemToList(QString &); ... } 

mainwindow.cpp

 void MainWindow::onDeviceChange(MSG * msg) { //code for detecting device here QString &driveLetter= getDriveLetter(mask); //try to emit QT signal here emit qm_diskInserted(driveLetter); } MainWindow::MainWindow(QWidget * parent=NULL) { ui.setupUi(this); QObject::connect(this, SIGNAL(qm_diskInserted(QString&)), this, SLOT(addItemToList(QString &)); } void MainWindow::addItemToList(QString &) { //more stuff here } 

somehow the addItemToList () slot is not being called, and I have to call it manually.
What am I doing wrong?

Thanks. PS:

By the way, is there a way to debug signals?
Those. How can I be sure that the signal is being emitted?

+1
source share
4 answers

For connection problems, always check the console for connection failure messages. Since Qt cannot determine if the connection makes sense before runtime, it notifies you of failures. You might think that this will work, but he just calmly talks about it in the console.

With Qt, it makes sense to constantly watch the console. Qt prints all kinds of error messages that may help when you have a problem.

+2
source

This is assumed to be Q_OBJECT. I think you also need to inherit QMainWindow.

+6
source

This is a long snapshot, but are you sure the onDeviceChange () method is being called?

EDIT

Classes that have the Q_OBJECT macro in their bodies must directly or indirectly inherit from QObject, but this is not the case in your code.

+3
source

Try making your virtual void signals and make sure your MainWindow class inherits (directly or indirectly) from QObject

EDIT

As mentioned in other comments, the macro should be Q_OBJECT

0
source

All Articles