[Development] QSpinBox 1 up/down click changes value by 2 steps

Carl Schumann schumann at fnal.gov
Mon Jul 22 19:35:24 CEST 2013


Hi,

I am having some problem with my use of Qt's QSpinBox.   In my 
application QSpinBox 1 up/down click changes the boxes value by 2 
steps.  (There are two separate valueChanged signals each by one 
step.)    The issue appears to be related to the amount of time taken by 
a slot attached to the valueChanged signal.     I have attached the 
small amount of source code needed to reproduce the issue, which is 
based on an example in the Qt 4 2nd Edition textbook.   I am 
experiencing this bug with Qt 4.8.4.  I don't have access to Qt 5 
because of incompatibilities with our Linux distribution.   I did try it 
on Qt 4.5.0 and got 3 steps for each click.

I am not sure what expectations are put on slot code.   Are slots 
expected to meet time constraints?  If no, have I found a bug Qt?

Sincerely,
Carl Schumann

-------------- next part --------------
#include <QApplication>
#include <QHBoxLayout>
#include <QSlider>
#include <QSpinBox>

#include "Model.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QWidget *window = new QWidget;
    window->setWindowTitle("Enter Your Age");

    QSpinBox *spinBox = new QSpinBox;
    QSlider *slider = new QSlider(Qt::Horizontal);
    spinBox->setRange(0, 130);
    slider->setRange(0, 130);

    QObject::connect(spinBox, SIGNAL(valueChanged(int)),
                     slider, SLOT(setValue(int)));
    QObject::connect(slider, SIGNAL(valueChanged(int)),
                     spinBox, SLOT(setValue(int)));
    spinBox->setValue(35);

    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget(spinBox);
    layout->addWidget(slider);
    window->setLayout(layout);

    Model model( spinBox->value() );
    QObject::connect(spinBox, SIGNAL(valueChanged(int)),
                     &model, SLOT(setAge(int)));

    window->show();

    return app.exec();
}

-------------- next part --------------
#include <iostream>
#include <unistd.h>

#include "Model.h"

Model::Model( int age_arg )
	: age( age_arg )
{
}

void Model::setAge( int age_arg )
{
	std::cout << "Age from " << this->age;
	sleep( 1 ); // Simulate a model that as bit of work to do to update itself
	this->age = age_arg;
	std::cout << " to " << this->age << std::endl;
}

-------------- next part --------------
#ifndef MODEL_H
#define MODEL_H

#include <QObject>

class Model : public QObject {
	Q_OBJECT

	public:
		Model( int age );

	public Q_SLOTS:
		void setAge( int age );

	private:
		int age;
};

#endif



More information about the Development mailing list