Multiple windows signal slot problem
-
Hi guys,
happy new year!
I am a very new bird with Qt and am writing a small project with 2 windows: a preference window and a bargraph window. In preference window you can change the configurations like color, size usw., and the bar in the bargraph window should be changed correspongdingly.
Now I am focus on the color change function. I added a color choose button on the preference window and with clicking it the slot function selectBarColor will be called and a QColorDialog can be opened. At the end of this funtion I tried to send the signal like this:
@connect(&d, SIGNAL(colorSelected(const QColor&)), &bargraph, SLOT(setColor(const QColor&)));@
with d is the QColorDialog object and bargraph is a Bargraph object.
And I wrote the slot setColor in Bargraph like this:
@void Bargraph::setColor(const QColor & color)
{
barColor = color;
update();
}@But it didn't work, it seems the setColor slot didn't get the signal.
What could be the problems here?
Thanks a lot!
-
connect only connects the signal and slots. You need to use the emit keyword to send the signal.
if the signal name is 'colorSelected' you should send the signal using
emit colorSelected(color)
Once you do this above slot will be called.
-
Hi and welcome to devnet,
Since "d" looks like it's created on the stack, are you sure it exists long enough to actually send the signal ?
Just to correct Dheerendra, you can also connect signals to signals. It's called signal forwarding. Not necessarily useful in this precise case but might be in the future.
-
Yes, Signal to Signal connect also can be used. What I meant in the above post was that, connect does not emit signal. It only connects. If he is assuming that it automatically emits signal it will be potential issue.
I think 'd' and 'bargraph' as stack variable are good observation from Galst. I feel this could be potential problem in your case. Make them as heap variables.
d = new classD
bargraph = new BarGraph. -
[quote author="Dheerendra" date="1389781569"]Yes, Signal to Signal connect also can be used. What I meant in the above post was that, connect does not emit signal. It only connects. If he is assuming that it automatically emits signal it will be potential issue.
I think 'd' and 'bargraph' as stack variable are good observation from Galst. I feel this could be potential problem in your case. Make them as heap variables.
d = new classD
bargraph = new BarGraph.[/quote]
Thanks a lot Dheerendra.
I checked the use of "emit" and wote
@emit d.colorSelected(bar_color);@But colorSelected(const QColor &) is a protected signal of QColorDialog. So it won't work if I wrote it in this way?
Could you please give me some further suggestions?Many thanks!
-
You can't emit a signal from another class
QColorDialog will emit the signal when needed
-
[quote author="SGaist" date="1389779303"]Hi and welcome to devnet,
Since "d" looks like it's created on the stack, are you sure it exists long enough to actually send the signal ?
Just to correct Dheerendra, you can also connect signals to signals. It's called signal forwarding. Not necessarily useful in this precise case but might be in the future.
[/quote]
Thank you very much SGaist! -
[quote author="SGaist" date="1389789056"]You can't emit a signal from another class
QColorDialog will emit the signal when needed[/quote]
Thanks a lot SGaist, it is because the "stack", I changed the position of the "connect" and it works now.
-
What do you mean by: "changed the position of the connect" ?
-
[quote author="SGaist" date="1389876303"]What do you mean by: "changed the position of the connect" ?[/quote]
Just right after the declaration of d
-
Looks like you might be doing something unusual with your code, can you post it ?