1

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?

Dr Deo
  • 4,650
  • 11
  • 43
  • 73

4 Answers4

6

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

anders
  • 772
  • 1
  • 10
  • 17
  • This is exactly the problem. Without Q_OBJECT *above* slot and signal declarations in your class the moc will not mocify those slots and signals and the magic crap that makes QObjects reflective (which is necessary for signals/slots in Qt) will not be declared and won't be generated in a moc.cpp file. – Edward Strange Dec 16 '10 at 17:51
3

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.

BЈовић
  • 62,405
  • 41
  • 173
  • 273
2

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.

Mike
  • 693
  • 3
  • 13
0

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

spbots
  • 1,513
  • 1
  • 15
  • 22