May 5, 2012

NameZero912 NameZero912
Lab Rat
2 posts

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!

3 replies

May 5, 2012

MuldeR MuldeR
Robot Herder
796 posts

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?

  1. void MyMainWindow::showEvent(QShowEvent *event)
  2. {
  3.     QMainWindow(event);
  4.     someLabel->setNum(someSlider->value());
  5. }

or:

  1. static const int initialValue = 42;
  2.  
  3. void MyMainWindow::showEvent(QShowEvent *event)
  4. {
  5.     QMainWindow(event);
  6.     someSlider->setValue(initialValue);
  7.     someLabel->setNum(initialValue);
  8. }

Yet another option would be adding a signal to your own class and connect that one to the QLabel’s signal:

  1. class MyMainWindow : public QMainWindow
  2. {
  3.     [...]
  4.  
  5. signals:
  6.     myValueChanged(int value);
  7.    
  8.     [...]
  9. }
  10.  
  11. [...]
  12.  
  13. void MyMainWindow::MyMainWindow( ... )
  14. {
  15.     [...]
  16.  
  17.     /* connect our own signal to the QSlider's signal */
  18.     connect(this, SIGNAL(myValueChanged(int)), someSlider, SIGNAL(valueChanged(int)));
  19.  
  20.     [...]
  21. }
  22.  
  23. void MyMainWindow::showEvent(QShowEvent *event)
  24. {
  25.     QMainWindow(event);
  26.     emit myValueChanged(42); //emit the "fake" signal
  27. }

 Signature 

My OpenSource software at: http://muldersoft.com/

Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

Go visit the coop: http://youtu.be/Jay-fG9eaYk

May 5, 2012

NameZero912 NameZero912
Lab Rat
2 posts

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.

  1. someSlider->setValue(initialValue);

May 5, 2012

MuldeR MuldeR
Robot Herder
796 posts
NameZero912 wrote:
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.

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 ;-)

NameZero912 wrote:
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);@

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.

 Signature 

My OpenSource software at: http://muldersoft.com/

Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

Go visit the coop: http://youtu.be/Jay-fG9eaYk

 
  ‹‹ Problems using activeX and MS Word      [SOLVED] Need some guidance on how to use QSortfilterproxymodel with QSqltablemodel to display checkboxes and perform filter operation ››

You must log in to post a reply. Not a member yet? Register here!