[Qt-interest] QAbstractSlider usage

Ole Streicher ole-usenet-spam at gmx.net
Tue May 26 15:06:12 CEST 2009


Hi,

I have a question regarding the use of a QAbstractSlider (QScrollBar,
QSlider): In my program, I have several ways to zoom something (f.e. a
diagram), one is a QSlider; the other an editable line. What I need now is to
connect the combobox as well as the slider to my zoom function, and to
update the other element with it. Something like

-----------------------8<--------------------------------------
QSlider slider;
QLineEdit lineedit;

connect(slider, SIGNAL('valueChanged(int)'), zoom_by_slider);
connect(lineedit, SIGNAL('editingFinisched(), zoom_by_number);

void zoomByNumber() {
  double zoomValue = getZoomValue(lineedit);
  int sliderPosition = getSliderPosition(zoomValue, slider);
  slider.setValue(sliderPosition);
  doMyZoom(zoomValue);
}

void zoomBySlider(int pos) {
  double zoomValue = getZoomValue(slider);
  QString zoomText = getZoomText(zoomValue);
  lineedit.setText(zoomText);
  doMyZoom(zoomValue)
}
-----------------------8<--------------------------------------

The problem with this is now that this is not working very well: If one
edited the lineedit, the editingFinished() signal is raised and thus
zoomByNumer() is called. This updates the slider position (*), which is
some rounded (integer) value and performs the required zoom.

In the moment the slider position is updated (*), another the slider
emits the valueChanged() signal that is connected to the zoomBySlider
slot. Therefore, the slider position is transformed back to a zoom value
(different from the first one, since it was stored as an int in
between). This new zoom value is now formatted and displayed in the
lineedit field. So, the output here may be different from the one just
entered. Also, the doMyZoom() function is called twice instead of once,
making the app slower than needed.

How can one avoid this signal ping-pong between different widgets? 

I tried already to check for slider.isSliderDown(), but this is not set
when one changes the value with the mouse wheel.

Best regards

Ole

P.S. since I didn't get any answer of my questions now: is there
something wrong with them?

P.P.S: Sorry if the example above is not correct C++; I just transferred
it from my Python code.




More information about the Qt-interest-old mailing list