PDA

View Full Version : unexpexted SIGNAL/SLOT behavior



eric
21st January 2008, 22:12
I have a situation where a signal needs to be emitted twice before it triggers slot action. Does anyone know why? This is what I have:

*.h file


class MyWidget : public QWidget
{
Q_OBJECT
.............
public slots:
void reSize();
signals:
void getSize();
...........
}


constructor in main.cpp


................
connect(this, SIGNAL(getSize()), this, SLOT(reSize()));
...............


functions.cpp


...........
void MyWidget::function1()
{
emit getSize();
emit getSize();//both emit's are needed!!
}

void MyWidget::reSize()
{
resize(x,y);//x and y are global variables
}
...........


The amazing thing is that I need to emit the signal twice. If it is emitted once, the resize function (in function2()) is not executed. ??

jpn
21st January 2008, 22:23
What does function2() have to do with this signal-slot connection? Are you also mixing QWidget::resize() and MyWidget::reSize()?

eric
21st January 2008, 22:36
Hi JPN,

Very sorry, my typo! I corrected it in the first post now. There is no function2(). Sorry again.


Are you also mixing QWidget::resize() and MyWidget::reSize()?
No, I don't mix resize() with reSize().

The above code works fine but I am confused why I have to emit signal twice.

jpn
21st January 2008, 22:42
Could you provide a minimal compilable example which reproduces the problem?

eric
21st January 2008, 22:55
This is, indeed, the best way to try to solve the mystery. The problem is that my program is very many functions long, all interacting with one another. It's very difficult to make a minimal program out of it. Besides, the minimal signal/slot without all the extra load always works fine.
My suspicion is that the signal needs to be emitted twice because the program is busy with something else and doesn't "notice" the signal. I have tried emitting the signal from various locations and also combining it with "QApplication::processEvents();" but no effect.
Is there a better way to increase priority of something in another way than doing: "QApplication::processEvents();"

wysota
21st January 2008, 23:54
I suggest taking a debugger and stepping through the code. I suspect you'll be able to find your problem easily then.

thomaspu
22nd January 2008, 03:24
if this is really in your constructor:

connect(this, SIGNAL(getSize()), this, SLOT(reSize()));
Then the signal thats being emitted lives in the object thats also recieving it. So why don't you just call the slot directly?

so... your functions.cpp file now has:

void MyWidget::function1()
{
reSize();
}

void MyWidget::reSize()
{
resize(x,y);//x and y are global variables
}
...........

Paul