Dr Deo
Dr Deo

Reputation: 4848

can't connect QT signal

i am new to QT and was trying to create a custom signal that would tell me a removable disk has been 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 slot addItemToList() isn't called and i have to call it manually.
What am i doing wrong?

Thanks. PS:

By the way is there any way of debugging signals?
Ie how can i be sure that a signal is emitted?

Upvotes: 1

Views: 3131

Answers (4)

Mike
Mike

Reputation: 702

With connection problems, always make sure that you check the console for messages about connect failures. Since Qt can't tell if a connection makes sense until runtime, it notifies you of failures there. You'd think it would crash, but it just quietly says these things in the console.

With Qt, it makes sense to watch the console always. Qt prints out all sorts of error messages that can help when you've got a problem.

Upvotes: 2

spbots
spbots

Reputation: 1553

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

EDIT

As mentioned in other comments, the macro should be Q_OBJECT

Upvotes: 0

anders
anders

Reputation: 782

It is at least supposed to be Q_OBJECT. I think you also need to inherit QMainWindow.

Upvotes: 6

BЈовић
BЈовић

Reputation: 64293

This is a long shot, but are you sure onDeviceChange() method is called?

EDIT

Classes which have Q_OBJECT macro in their body needs to inherit directly or indirectly from QObject, and in your code it is not the case.

Upvotes: 3

Related Questions