PDA

View Full Version : signal ending nowhere...



miwarre
2nd September 2009, 13:36
I thought I know the basics of signals and slots but this keeps eluding me. I searched the fora, but none of the suggestions in similar cases were applicable or solved the problem. This is the scenario:

1) my main window is a QWidget
2) inside it, potentially through a hierarchy of container widgets, there is a number of QLabel-derived widgets
3) when one of the latter is clicked I want a slot in the main window to be called, with some parameters (these params are 1 of the reasons I did not use the standard clicked() signal of QPushButton),

Some code. (Part of) the QLabel-derived widget definition:

class CHouse : public QLabel
{
Q_OBJECT
[...]
protected:
void mouseReleaseEvent(QMouseEvent * pEvent); // reimplemented from QWidget
signals:
void houseClicked(int nHouse);
[...]
};
In mouseReleaseEvent(), the signal is emitted:

void CHouse::mouseReleaseEvent(QMouseEvent * pEvent)
{
if( /* some tests on internal object data */)
emit houseClicked(m_nHouse);
}

The main window definition:

class CMainWindow : public QWidget
{
Q_OBJECT

[...]
CHouse * m_pHouse[MNC_MAX_NUM_OF_ROWS][MNC_MAX_NUM_OF_COLS];
public slots:
void MoveDo(int nHouse);
[...]
};

This is where the children widgets are created and their signal connected to the main window slot:

bool CMainWindow::InitUI(void)
{ int nCol, nRow;
int nHouse;
bool bRes;

[...]
for(nRow=0; nRow < m_nNumOfRows; nRow++)
{ for(nCol=0; nCol < m_nNumOfCols; nCol++)
{ nHouse = ...;
if( (m_pHouse[nRow][nCol]=new CHouse(nHouse, this)) != NULL)
bRes = connect(m_pHouse[nRow][nCol], SIGNAL(houseClicked(int)), this, SLOT(MoveDo(int)));
}
}
[...]
}

Result:
1) The code compiles correctly
2) At run time connect() returns true
3) CHouse::mouseReleaseEvent() is properly called and the emit houseClicked(m_nHouse); line executed when needed
4) no warning or error is issued while debugging
BUT
5) MoveDo() slot is never called.

I have tried replacing the SLOT(...) with, say, qApp, SLOT(quit()) or with another dummy slot added to CHouse: in no case the connected slot was called.

I traced the emit source code down to qobject.cpp, line 3047 in function void QMetaObject::activate(QObject *sender, int from_signal_index, int to_signal_index, void **argv) where the connection list for the sender appears to be empty!

Other signals connected from other children widgets (QPushButton in this case) to other slots of the main window do work.

What am I doing wrong?

Thanks,
M.

spirit
2nd September 2009, 13:40
can you provide a minimal compilable example which reproduces the problem?

miwarre
2nd September 2009, 16:38
Thanks for the reply, spirit!

things are getting more and more interesting: when I prepared a bare-bones compilable sample and I tried it, of course it WORKED!

Now, the homework is to add pieces back and see where the problem pops up again. Back to coding...

Thanks,

M.