Having manual/virtual events for some widgets after window initialization
-
Hi,
I'm very new to QT4, so this question might be stupid, sorry. I'm using Qt 4.8.1 on Windows. I'm using QtDesigner for creating my GUIs. It has e.g. some sliders, and since (for a reason I cannot fathom) sliders don't have labels, I have to manually create a QLabel next to them. QtDesigner allows to set the signal->slot mapping, such that a change in the slider (valueChanged(int)) causes the label's setNum() to be called.
However, it is necessary that the user ACTUALLY changes the slider's value for the label to be set. I'd like to know whether there is a clean solution to "simulate" such a change of value (without actually changing it), such that e.g. the corresponding label will update, such that this is done once after the main window has successfully loaded.
My current, unclean solution is something like calling slider.setValue(slider.value()+1)); slider.setValue(slider.value()-1)); which is quite horrible..
Cheers!
-
You can simply call the QLabel's setNum() slot like any other function.
Why not call it from your the constructor of your class? Or from the showEvent() method?
@void MyMainWindow::showEvent(QShowEvent *event)
{
QMainWindow(event);
someLabel->setNum(someSlider->value());
}@
or:@static const int initialValue = 42;
void MyMainWindow::showEvent(QShowEvent *event)
{
QMainWindow(event);
someSlider->setValue(initialValue);
someLabel->setNum(initialValue);
}@
Yet another option would be adding a signal to your own class and connect that one to the QLabel's signal:@class MyMainWindow : public QMainWindow
{
[...]signals:
myValueChanged(int value);[...]
}
[...]
void MyMainWindow::MyMainWindow( ... )
{
[...]/* connect our own signal to the QSlider's signal */ connect(this, SIGNAL(myValueChanged(int)), someSlider, SIGNAL(valueChanged(int))); [...]
}
void MyMainWindow::showEvent(QShowEvent *event)
{
QMainWindow(event);
emit myValueChanged(42); //emit the "fake" signal
}@ -
Thanks for the fast response. Well, in my case I'm connecting the slider's valueChanged(int) signal to the label's setNum() slot in the QtDesigner (consequently, the generated cpp files have this connect(...) statement), but then in my own code I also connect the slider's valueChanged(int) signal to my own slot (in which I take the new value and do something else with it). I was looking for a way to have both slots (my own, and labe's setNum()) to be triggered on start-up of the application.
From reading your code samples, it occurred to me that it's probably not a good approach to set the default value in the QtDesigner, trying to "push" this default value into the slots connected to slider's valueChanged(int). Instead I should simply have something that is not the default value in QtDesigner, and set the default value in code instead, i.e. @someSlider->setValue(initialValue);@
-
[quote author="NameZero912" date="1336228968"]Thanks for the fast response. Well, in my case I'm connecting the slider's valueChanged(int) signal to the label's setNum() slot in the QtDesigner (consequently, the generated cpp files have this connect(...) statement), but then in my own code I also connect the slider's valueChanged(int) signal to my own slot (in which I take the new value and do something else with it). I was looking for a way to have both slots (my own, and labe's setNum()) to be triggered on start-up of the application.[/quote]
This can be done, for example, by adding a suitable signal to your own class. Then you can connect the signal of your own class to the QLabel's signals. Qt can connect signals to signals! Then emitting your own signal will trigger the signal that it is connected to (e.g. the signal of the QLabel). The QLabel's signal in turn will trigger all slots connected to it - as you desire. Look at the code of my previous posting for an example...
Of course you may simply call all slot's you want to trigger directly ;-)
[quote author="NameZero912" date="1336228968"]From reading your code samples, it occurred to me that it's probably not a good approach to set the default value in the QtDesigner, trying to "push" this default value into the slots connected to slider's valueChanged(int). Instead I should simply have something that is not the default value in QtDesigner, and set the default value in code instead, i.e. @someSlider->setValue(initialValue);@[/quote]
After all it may be desirable to "remember" the last value in an INI file and load it from there at the next application startup. If you do this, you will have to overwrite the value somewhere in your own initialization code anyway. Nonetheless you can keep the "initial" value from QDesigner with code like this:
someLabel->setNum(someSlider->value());
someOtherSlotThatNeedsInitialization(someSlider->value());someSlider->value() will initially contain the value you have set in QDesigner, given that setupUi() has been called.